ich.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  1. /*
  2. * Copyright (c) 2011-12 The Chromium OS Authors.
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * This file is derived from the flashrom project.
  7. */
  8. #include <common.h>
  9. #include <dm.h>
  10. #include <errno.h>
  11. #include <malloc.h>
  12. #include <pch.h>
  13. #include <pci.h>
  14. #include <pci_ids.h>
  15. #include <spi.h>
  16. #include <asm/io.h>
  17. #include "ich.h"
  18. DECLARE_GLOBAL_DATA_PTR;
  19. #ifdef DEBUG_TRACE
  20. #define debug_trace(fmt, args...) debug(fmt, ##args)
  21. #else
  22. #define debug_trace(x, args...)
  23. #endif
  24. static u8 ich_readb(struct ich_spi_priv *priv, int reg)
  25. {
  26. u8 value = readb(priv->base + reg);
  27. debug_trace("read %2.2x from %4.4x\n", value, reg);
  28. return value;
  29. }
  30. static u16 ich_readw(struct ich_spi_priv *priv, int reg)
  31. {
  32. u16 value = readw(priv->base + reg);
  33. debug_trace("read %4.4x from %4.4x\n", value, reg);
  34. return value;
  35. }
  36. static u32 ich_readl(struct ich_spi_priv *priv, int reg)
  37. {
  38. u32 value = readl(priv->base + reg);
  39. debug_trace("read %8.8x from %4.4x\n", value, reg);
  40. return value;
  41. }
  42. static void ich_writeb(struct ich_spi_priv *priv, u8 value, int reg)
  43. {
  44. writeb(value, priv->base + reg);
  45. debug_trace("wrote %2.2x to %4.4x\n", value, reg);
  46. }
  47. static void ich_writew(struct ich_spi_priv *priv, u16 value, int reg)
  48. {
  49. writew(value, priv->base + reg);
  50. debug_trace("wrote %4.4x to %4.4x\n", value, reg);
  51. }
  52. static void ich_writel(struct ich_spi_priv *priv, u32 value, int reg)
  53. {
  54. writel(value, priv->base + reg);
  55. debug_trace("wrote %8.8x to %4.4x\n", value, reg);
  56. }
  57. static void write_reg(struct ich_spi_priv *priv, const void *value,
  58. int dest_reg, uint32_t size)
  59. {
  60. memcpy_toio(priv->base + dest_reg, value, size);
  61. }
  62. static void read_reg(struct ich_spi_priv *priv, int src_reg, void *value,
  63. uint32_t size)
  64. {
  65. memcpy_fromio(value, priv->base + src_reg, size);
  66. }
  67. static void ich_set_bbar(struct ich_spi_priv *ctlr, uint32_t minaddr)
  68. {
  69. const uint32_t bbar_mask = 0x00ffff00;
  70. uint32_t ichspi_bbar;
  71. minaddr &= bbar_mask;
  72. ichspi_bbar = ich_readl(ctlr, ctlr->bbar) & ~bbar_mask;
  73. ichspi_bbar |= minaddr;
  74. ich_writel(ctlr, ichspi_bbar, ctlr->bbar);
  75. }
  76. /* @return 1 if the SPI flash supports the 33MHz speed */
  77. static int ich9_can_do_33mhz(struct udevice *dev)
  78. {
  79. u32 fdod, speed;
  80. /* Observe SPI Descriptor Component Section 0 */
  81. dm_pci_write_config32(dev->parent, 0xb0, 0x1000);
  82. /* Extract the Write/Erase SPI Frequency from descriptor */
  83. dm_pci_read_config32(dev->parent, 0xb4, &fdod);
  84. /* Bits 23:21 have the fast read clock frequency, 0=20MHz, 1=33MHz */
  85. speed = (fdod >> 21) & 7;
  86. return speed == 1;
  87. }
  88. static int ich_init_controller(struct udevice *dev,
  89. struct ich_spi_platdata *plat,
  90. struct ich_spi_priv *ctlr)
  91. {
  92. ulong sbase_addr;
  93. void *sbase;
  94. /* SBASE is similar */
  95. pch_get_spi_base(dev->parent, &sbase_addr);
  96. sbase = (void *)sbase_addr;
  97. debug("%s: sbase=%p\n", __func__, sbase);
  98. if (plat->ich_version == ICHV_7) {
  99. struct ich7_spi_regs *ich7_spi = sbase;
  100. ctlr->opmenu = offsetof(struct ich7_spi_regs, opmenu);
  101. ctlr->menubytes = sizeof(ich7_spi->opmenu);
  102. ctlr->optype = offsetof(struct ich7_spi_regs, optype);
  103. ctlr->addr = offsetof(struct ich7_spi_regs, spia);
  104. ctlr->data = offsetof(struct ich7_spi_regs, spid);
  105. ctlr->databytes = sizeof(ich7_spi->spid);
  106. ctlr->status = offsetof(struct ich7_spi_regs, spis);
  107. ctlr->control = offsetof(struct ich7_spi_regs, spic);
  108. ctlr->bbar = offsetof(struct ich7_spi_regs, bbar);
  109. ctlr->preop = offsetof(struct ich7_spi_regs, preop);
  110. ctlr->base = ich7_spi;
  111. } else if (plat->ich_version == ICHV_9) {
  112. struct ich9_spi_regs *ich9_spi = sbase;
  113. ctlr->opmenu = offsetof(struct ich9_spi_regs, opmenu);
  114. ctlr->menubytes = sizeof(ich9_spi->opmenu);
  115. ctlr->optype = offsetof(struct ich9_spi_regs, optype);
  116. ctlr->addr = offsetof(struct ich9_spi_regs, faddr);
  117. ctlr->data = offsetof(struct ich9_spi_regs, fdata);
  118. ctlr->databytes = sizeof(ich9_spi->fdata);
  119. ctlr->status = offsetof(struct ich9_spi_regs, ssfs);
  120. ctlr->control = offsetof(struct ich9_spi_regs, ssfc);
  121. ctlr->speed = ctlr->control + 2;
  122. ctlr->bbar = offsetof(struct ich9_spi_regs, bbar);
  123. ctlr->preop = offsetof(struct ich9_spi_regs, preop);
  124. ctlr->bcr = offsetof(struct ich9_spi_regs, bcr);
  125. ctlr->pr = &ich9_spi->pr[0];
  126. ctlr->base = ich9_spi;
  127. } else {
  128. debug("ICH SPI: Unrecognised ICH version %d\n",
  129. plat->ich_version);
  130. return -EINVAL;
  131. }
  132. /* Work out the maximum speed we can support */
  133. ctlr->max_speed = 20000000;
  134. if (plat->ich_version == ICHV_9 && ich9_can_do_33mhz(dev))
  135. ctlr->max_speed = 33000000;
  136. debug("ICH SPI: Version ID %d detected at %p, speed %ld\n",
  137. plat->ich_version, ctlr->base, ctlr->max_speed);
  138. ich_set_bbar(ctlr, 0);
  139. return 0;
  140. }
  141. static inline void spi_use_out(struct spi_trans *trans, unsigned bytes)
  142. {
  143. trans->out += bytes;
  144. trans->bytesout -= bytes;
  145. }
  146. static inline void spi_use_in(struct spi_trans *trans, unsigned bytes)
  147. {
  148. trans->in += bytes;
  149. trans->bytesin -= bytes;
  150. }
  151. static void spi_lock_down(struct ich_spi_platdata *plat, void *sbase)
  152. {
  153. if (plat->ich_version == ICHV_7) {
  154. struct ich7_spi_regs *ich7_spi = sbase;
  155. setbits_le16(&ich7_spi->spis, SPIS_LOCK);
  156. } else if (plat->ich_version == ICHV_9) {
  157. struct ich9_spi_regs *ich9_spi = sbase;
  158. setbits_le16(&ich9_spi->hsfs, HSFS_FLOCKDN);
  159. }
  160. }
  161. static bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
  162. {
  163. int lock = 0;
  164. if (plat->ich_version == ICHV_7) {
  165. struct ich7_spi_regs *ich7_spi = sbase;
  166. lock = readw(&ich7_spi->spis) & SPIS_LOCK;
  167. } else if (plat->ich_version == ICHV_9) {
  168. struct ich9_spi_regs *ich9_spi = sbase;
  169. lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
  170. }
  171. return lock != 0;
  172. }
  173. static void spi_setup_type(struct spi_trans *trans, int data_bytes)
  174. {
  175. trans->type = 0xFF;
  176. /* Try to guess spi type from read/write sizes */
  177. if (trans->bytesin == 0) {
  178. if (trans->bytesout + data_bytes > 4)
  179. /*
  180. * If bytesin = 0 and bytesout > 4, we presume this is
  181. * a write data operation, which is accompanied by an
  182. * address.
  183. */
  184. trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
  185. else
  186. trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
  187. return;
  188. }
  189. if (trans->bytesout == 1) { /* and bytesin is > 0 */
  190. trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
  191. return;
  192. }
  193. if (trans->bytesout == 4) /* and bytesin is > 0 */
  194. trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  195. /* Fast read command is called with 5 bytes instead of 4 */
  196. if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
  197. trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  198. --trans->bytesout;
  199. }
  200. }
  201. static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
  202. bool lock)
  203. {
  204. uint16_t optypes;
  205. uint8_t opmenu[ctlr->menubytes];
  206. trans->opcode = trans->out[0];
  207. spi_use_out(trans, 1);
  208. if (!lock) {
  209. /* The lock is off, so just use index 0. */
  210. ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
  211. optypes = ich_readw(ctlr, ctlr->optype);
  212. optypes = (optypes & 0xfffc) | (trans->type & 0x3);
  213. ich_writew(ctlr, optypes, ctlr->optype);
  214. return 0;
  215. } else {
  216. /* The lock is on. See if what we need is on the menu. */
  217. uint8_t optype;
  218. uint16_t opcode_index;
  219. /* Write Enable is handled as atomic prefix */
  220. if (trans->opcode == SPI_OPCODE_WREN)
  221. return 0;
  222. read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
  223. for (opcode_index = 0; opcode_index < ctlr->menubytes;
  224. opcode_index++) {
  225. if (opmenu[opcode_index] == trans->opcode)
  226. break;
  227. }
  228. if (opcode_index == ctlr->menubytes) {
  229. printf("ICH SPI: Opcode %x not found\n",
  230. trans->opcode);
  231. return -EINVAL;
  232. }
  233. optypes = ich_readw(ctlr, ctlr->optype);
  234. optype = (optypes >> (opcode_index * 2)) & 0x3;
  235. if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
  236. optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
  237. trans->bytesout >= 3) {
  238. /* We guessed wrong earlier. Fix it up. */
  239. trans->type = optype;
  240. }
  241. if (optype != trans->type) {
  242. printf("ICH SPI: Transaction doesn't fit type %d\n",
  243. optype);
  244. return -ENOSPC;
  245. }
  246. return opcode_index;
  247. }
  248. }
  249. static int spi_setup_offset(struct spi_trans *trans)
  250. {
  251. /* Separate the SPI address and data */
  252. switch (trans->type) {
  253. case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
  254. case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
  255. return 0;
  256. case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
  257. case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
  258. trans->offset = ((uint32_t)trans->out[0] << 16) |
  259. ((uint32_t)trans->out[1] << 8) |
  260. ((uint32_t)trans->out[2] << 0);
  261. spi_use_out(trans, 3);
  262. return 1;
  263. default:
  264. printf("Unrecognized SPI transaction type %#x\n", trans->type);
  265. return -EPROTO;
  266. }
  267. }
  268. /*
  269. * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
  270. * below is true) or 0. In case the wait was for the bit(s) to set - write
  271. * those bits back, which would cause resetting them.
  272. *
  273. * Return the last read status value on success or -1 on failure.
  274. */
  275. static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
  276. int wait_til_set)
  277. {
  278. int timeout = 600000; /* This will result in 6s */
  279. u16 status = 0;
  280. while (timeout--) {
  281. status = ich_readw(ctlr, ctlr->status);
  282. if (wait_til_set ^ ((status & bitmask) == 0)) {
  283. if (wait_til_set) {
  284. ich_writew(ctlr, status & bitmask,
  285. ctlr->status);
  286. }
  287. return status;
  288. }
  289. udelay(10);
  290. }
  291. printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
  292. status, bitmask);
  293. return -ETIMEDOUT;
  294. }
  295. void ich_spi_config_opcode(struct udevice *dev)
  296. {
  297. struct ich_spi_priv *ctlr = dev_get_priv(dev);
  298. /*
  299. * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
  300. * to prevent accidental or intentional writes. Before they get
  301. * locked down, these registers should be initialized properly.
  302. */
  303. ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
  304. ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
  305. ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
  306. ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
  307. }
  308. static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
  309. const void *dout, void *din, unsigned long flags)
  310. {
  311. struct udevice *bus = dev_get_parent(dev);
  312. struct ich_spi_platdata *plat = dev_get_platdata(bus);
  313. struct ich_spi_priv *ctlr = dev_get_priv(bus);
  314. uint16_t control;
  315. int16_t opcode_index;
  316. int with_address;
  317. int status;
  318. int bytes = bitlen / 8;
  319. struct spi_trans *trans = &ctlr->trans;
  320. unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
  321. int using_cmd = 0;
  322. bool lock = spi_lock_status(plat, ctlr->base);
  323. int ret;
  324. /* We don't support writing partial bytes */
  325. if (bitlen % 8) {
  326. debug("ICH SPI: Accessing partial bytes not supported\n");
  327. return -EPROTONOSUPPORT;
  328. }
  329. /* An empty end transaction can be ignored */
  330. if (type == SPI_XFER_END && !dout && !din)
  331. return 0;
  332. if (type & SPI_XFER_BEGIN)
  333. memset(trans, '\0', sizeof(*trans));
  334. /* Dp we need to come back later to finish it? */
  335. if (dout && type == SPI_XFER_BEGIN) {
  336. if (bytes > ICH_MAX_CMD_LEN) {
  337. debug("ICH SPI: Command length limit exceeded\n");
  338. return -ENOSPC;
  339. }
  340. memcpy(trans->cmd, dout, bytes);
  341. trans->cmd_len = bytes;
  342. debug_trace("ICH SPI: Saved %d bytes\n", bytes);
  343. return 0;
  344. }
  345. /*
  346. * We process a 'middle' spi_xfer() call, which has no
  347. * SPI_XFER_BEGIN/END, as an independent transaction as if it had
  348. * an end. We therefore repeat the command. This is because ICH
  349. * seems to have no support for this, or because interest (in digging
  350. * out the details and creating a special case in the code) is low.
  351. */
  352. if (trans->cmd_len) {
  353. trans->out = trans->cmd;
  354. trans->bytesout = trans->cmd_len;
  355. using_cmd = 1;
  356. debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
  357. } else {
  358. trans->out = dout;
  359. trans->bytesout = dout ? bytes : 0;
  360. }
  361. trans->in = din;
  362. trans->bytesin = din ? bytes : 0;
  363. /* There has to always at least be an opcode */
  364. if (!trans->bytesout) {
  365. debug("ICH SPI: No opcode for transfer\n");
  366. return -EPROTO;
  367. }
  368. ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
  369. if (ret < 0)
  370. return ret;
  371. if (plat->ich_version == ICHV_7)
  372. ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
  373. else
  374. ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
  375. spi_setup_type(trans, using_cmd ? bytes : 0);
  376. opcode_index = spi_setup_opcode(ctlr, trans, lock);
  377. if (opcode_index < 0)
  378. return -EINVAL;
  379. with_address = spi_setup_offset(trans);
  380. if (with_address < 0)
  381. return -EINVAL;
  382. if (trans->opcode == SPI_OPCODE_WREN) {
  383. /*
  384. * Treat Write Enable as Atomic Pre-Op if possible
  385. * in order to prevent the Management Engine from
  386. * issuing a transaction between WREN and DATA.
  387. */
  388. if (!lock)
  389. ich_writew(ctlr, trans->opcode, ctlr->preop);
  390. return 0;
  391. }
  392. if (ctlr->speed && ctlr->max_speed >= 33000000) {
  393. int byte;
  394. byte = ich_readb(ctlr, ctlr->speed);
  395. if (ctlr->cur_speed >= 33000000)
  396. byte |= SSFC_SCF_33MHZ;
  397. else
  398. byte &= ~SSFC_SCF_33MHZ;
  399. ich_writeb(ctlr, byte, ctlr->speed);
  400. }
  401. /* See if we have used up the command data */
  402. if (using_cmd && dout && bytes) {
  403. trans->out = dout;
  404. trans->bytesout = bytes;
  405. debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
  406. }
  407. /* Preset control fields */
  408. control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
  409. /* Issue atomic preop cycle if needed */
  410. if (ich_readw(ctlr, ctlr->preop))
  411. control |= SPIC_ACS;
  412. if (!trans->bytesout && !trans->bytesin) {
  413. /* SPI addresses are 24 bit only */
  414. if (with_address) {
  415. ich_writel(ctlr, trans->offset & 0x00FFFFFF,
  416. ctlr->addr);
  417. }
  418. /*
  419. * This is a 'no data' command (like Write Enable), its
  420. * bitesout size was 1, decremented to zero while executing
  421. * spi_setup_opcode() above. Tell the chip to send the
  422. * command.
  423. */
  424. ich_writew(ctlr, control, ctlr->control);
  425. /* wait for the result */
  426. status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
  427. if (status < 0)
  428. return status;
  429. if (status & SPIS_FCERR) {
  430. debug("ICH SPI: Command transaction error\n");
  431. return -EIO;
  432. }
  433. return 0;
  434. }
  435. /*
  436. * Check if this is a write command atempting to transfer more bytes
  437. * than the controller can handle. Iterations for writes are not
  438. * supported here because each SPI write command needs to be preceded
  439. * and followed by other SPI commands, and this sequence is controlled
  440. * by the SPI chip driver.
  441. */
  442. if (trans->bytesout > ctlr->databytes) {
  443. debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
  444. return -EPROTO;
  445. }
  446. /*
  447. * Read or write up to databytes bytes at a time until everything has
  448. * been sent.
  449. */
  450. while (trans->bytesout || trans->bytesin) {
  451. uint32_t data_length;
  452. /* SPI addresses are 24 bit only */
  453. ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
  454. if (trans->bytesout)
  455. data_length = min(trans->bytesout, ctlr->databytes);
  456. else
  457. data_length = min(trans->bytesin, ctlr->databytes);
  458. /* Program data into FDATA0 to N */
  459. if (trans->bytesout) {
  460. write_reg(ctlr, trans->out, ctlr->data, data_length);
  461. spi_use_out(trans, data_length);
  462. if (with_address)
  463. trans->offset += data_length;
  464. }
  465. /* Add proper control fields' values */
  466. control &= ~((ctlr->databytes - 1) << 8);
  467. control |= SPIC_DS;
  468. control |= (data_length - 1) << 8;
  469. /* write it */
  470. ich_writew(ctlr, control, ctlr->control);
  471. /* Wait for Cycle Done Status or Flash Cycle Error */
  472. status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
  473. if (status < 0)
  474. return status;
  475. if (status & SPIS_FCERR) {
  476. debug("ICH SPI: Data transaction error %x\n", status);
  477. return -EIO;
  478. }
  479. if (trans->bytesin) {
  480. read_reg(ctlr, ctlr->data, trans->in, data_length);
  481. spi_use_in(trans, data_length);
  482. if (with_address)
  483. trans->offset += data_length;
  484. }
  485. }
  486. /* Clear atomic preop now that xfer is done */
  487. if (!lock)
  488. ich_writew(ctlr, 0, ctlr->preop);
  489. return 0;
  490. }
  491. static int ich_spi_probe(struct udevice *dev)
  492. {
  493. struct ich_spi_platdata *plat = dev_get_platdata(dev);
  494. struct ich_spi_priv *priv = dev_get_priv(dev);
  495. uint8_t bios_cntl;
  496. int ret;
  497. ret = ich_init_controller(dev, plat, priv);
  498. if (ret)
  499. return ret;
  500. /* Disable the BIOS write protect so write commands are allowed */
  501. ret = pch_set_spi_protect(dev->parent, false);
  502. if (ret == -ENOSYS) {
  503. bios_cntl = ich_readb(priv, priv->bcr);
  504. bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
  505. bios_cntl |= 1; /* Write Protect Disable (WPD) */
  506. ich_writeb(priv, bios_cntl, priv->bcr);
  507. } else if (ret) {
  508. debug("%s: Failed to disable write-protect: err=%d\n",
  509. __func__, ret);
  510. return ret;
  511. }
  512. /* Lock down SPI controller settings if required */
  513. if (plat->lockdown) {
  514. ich_spi_config_opcode(dev);
  515. spi_lock_down(plat, priv->base);
  516. }
  517. priv->cur_speed = priv->max_speed;
  518. return 0;
  519. }
  520. static int ich_spi_remove(struct udevice *bus)
  521. {
  522. /*
  523. * Configure SPI controller so that the Linux MTD driver can fully
  524. * access the SPI NOR chip
  525. */
  526. ich_spi_config_opcode(bus);
  527. return 0;
  528. }
  529. static int ich_spi_set_speed(struct udevice *bus, uint speed)
  530. {
  531. struct ich_spi_priv *priv = dev_get_priv(bus);
  532. priv->cur_speed = speed;
  533. return 0;
  534. }
  535. static int ich_spi_set_mode(struct udevice *bus, uint mode)
  536. {
  537. debug("%s: mode=%d\n", __func__, mode);
  538. return 0;
  539. }
  540. static int ich_spi_child_pre_probe(struct udevice *dev)
  541. {
  542. struct udevice *bus = dev_get_parent(dev);
  543. struct ich_spi_platdata *plat = dev_get_platdata(bus);
  544. struct ich_spi_priv *priv = dev_get_priv(bus);
  545. struct spi_slave *slave = dev_get_parent_priv(dev);
  546. /*
  547. * Yes this controller can only write a small number of bytes at
  548. * once! The limit is typically 64 bytes.
  549. */
  550. slave->max_write_size = priv->databytes;
  551. /*
  552. * ICH 7 SPI controller only supports array read command
  553. * and byte program command for SST flash
  554. */
  555. if (plat->ich_version == ICHV_7)
  556. slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
  557. return 0;
  558. }
  559. static int ich_spi_ofdata_to_platdata(struct udevice *dev)
  560. {
  561. struct ich_spi_platdata *plat = dev_get_platdata(dev);
  562. int node = dev_of_offset(dev);
  563. int ret;
  564. ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
  565. if (ret == 0) {
  566. plat->ich_version = ICHV_7;
  567. } else {
  568. ret = fdt_node_check_compatible(gd->fdt_blob, node,
  569. "intel,ich9-spi");
  570. if (ret == 0)
  571. plat->ich_version = ICHV_9;
  572. }
  573. plat->lockdown = fdtdec_get_bool(gd->fdt_blob, node,
  574. "intel,spi-lock-down");
  575. return ret;
  576. }
  577. static const struct dm_spi_ops ich_spi_ops = {
  578. .xfer = ich_spi_xfer,
  579. .set_speed = ich_spi_set_speed,
  580. .set_mode = ich_spi_set_mode,
  581. /*
  582. * cs_info is not needed, since we require all chip selects to be
  583. * in the device tree explicitly
  584. */
  585. };
  586. static const struct udevice_id ich_spi_ids[] = {
  587. { .compatible = "intel,ich7-spi" },
  588. { .compatible = "intel,ich9-spi" },
  589. { }
  590. };
  591. U_BOOT_DRIVER(ich_spi) = {
  592. .name = "ich_spi",
  593. .id = UCLASS_SPI,
  594. .of_match = ich_spi_ids,
  595. .ops = &ich_spi_ops,
  596. .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
  597. .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
  598. .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
  599. .child_pre_probe = ich_spi_child_pre_probe,
  600. .probe = ich_spi_probe,
  601. .remove = ich_spi_remove,
  602. .flags = DM_FLAG_OS_PREPARE,
  603. };