ich.c 19 KB

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