ich.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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 bool spi_lock_status(struct ich_spi_platdata *plat, void *sbase)
  152. {
  153. int lock = 0;
  154. if (plat->ich_version == ICHV_7) {
  155. struct ich7_spi_regs *ich7_spi = sbase;
  156. lock = readw(&ich7_spi->spis) & SPIS_LOCK;
  157. } else if (plat->ich_version == ICHV_9) {
  158. struct ich9_spi_regs *ich9_spi = sbase;
  159. lock = readw(&ich9_spi->hsfs) & HSFS_FLOCKDN;
  160. }
  161. return lock != 0;
  162. }
  163. static void spi_setup_type(struct spi_trans *trans, int data_bytes)
  164. {
  165. trans->type = 0xFF;
  166. /* Try to guess spi type from read/write sizes */
  167. if (trans->bytesin == 0) {
  168. if (trans->bytesout + data_bytes > 4)
  169. /*
  170. * If bytesin = 0 and bytesout > 4, we presume this is
  171. * a write data operation, which is accompanied by an
  172. * address.
  173. */
  174. trans->type = SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS;
  175. else
  176. trans->type = SPI_OPCODE_TYPE_WRITE_NO_ADDRESS;
  177. return;
  178. }
  179. if (trans->bytesout == 1) { /* and bytesin is > 0 */
  180. trans->type = SPI_OPCODE_TYPE_READ_NO_ADDRESS;
  181. return;
  182. }
  183. if (trans->bytesout == 4) /* and bytesin is > 0 */
  184. trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  185. /* Fast read command is called with 5 bytes instead of 4 */
  186. if (trans->out[0] == SPI_OPCODE_FAST_READ && trans->bytesout == 5) {
  187. trans->type = SPI_OPCODE_TYPE_READ_WITH_ADDRESS;
  188. --trans->bytesout;
  189. }
  190. }
  191. static int spi_setup_opcode(struct ich_spi_priv *ctlr, struct spi_trans *trans,
  192. bool lock)
  193. {
  194. uint16_t optypes;
  195. uint8_t opmenu[ctlr->menubytes];
  196. trans->opcode = trans->out[0];
  197. spi_use_out(trans, 1);
  198. if (!lock) {
  199. /* The lock is off, so just use index 0. */
  200. ich_writeb(ctlr, trans->opcode, ctlr->opmenu);
  201. optypes = ich_readw(ctlr, ctlr->optype);
  202. optypes = (optypes & 0xfffc) | (trans->type & 0x3);
  203. ich_writew(ctlr, optypes, ctlr->optype);
  204. return 0;
  205. } else {
  206. /* The lock is on. See if what we need is on the menu. */
  207. uint8_t optype;
  208. uint16_t opcode_index;
  209. /* Write Enable is handled as atomic prefix */
  210. if (trans->opcode == SPI_OPCODE_WREN)
  211. return 0;
  212. read_reg(ctlr, ctlr->opmenu, opmenu, sizeof(opmenu));
  213. for (opcode_index = 0; opcode_index < ctlr->menubytes;
  214. opcode_index++) {
  215. if (opmenu[opcode_index] == trans->opcode)
  216. break;
  217. }
  218. if (opcode_index == ctlr->menubytes) {
  219. printf("ICH SPI: Opcode %x not found\n",
  220. trans->opcode);
  221. return -EINVAL;
  222. }
  223. optypes = ich_readw(ctlr, ctlr->optype);
  224. optype = (optypes >> (opcode_index * 2)) & 0x3;
  225. if (trans->type == SPI_OPCODE_TYPE_WRITE_NO_ADDRESS &&
  226. optype == SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS &&
  227. trans->bytesout >= 3) {
  228. /* We guessed wrong earlier. Fix it up. */
  229. trans->type = optype;
  230. }
  231. if (optype != trans->type) {
  232. printf("ICH SPI: Transaction doesn't fit type %d\n",
  233. optype);
  234. return -ENOSPC;
  235. }
  236. return opcode_index;
  237. }
  238. }
  239. static int spi_setup_offset(struct spi_trans *trans)
  240. {
  241. /* Separate the SPI address and data */
  242. switch (trans->type) {
  243. case SPI_OPCODE_TYPE_READ_NO_ADDRESS:
  244. case SPI_OPCODE_TYPE_WRITE_NO_ADDRESS:
  245. return 0;
  246. case SPI_OPCODE_TYPE_READ_WITH_ADDRESS:
  247. case SPI_OPCODE_TYPE_WRITE_WITH_ADDRESS:
  248. trans->offset = ((uint32_t)trans->out[0] << 16) |
  249. ((uint32_t)trans->out[1] << 8) |
  250. ((uint32_t)trans->out[2] << 0);
  251. spi_use_out(trans, 3);
  252. return 1;
  253. default:
  254. printf("Unrecognized SPI transaction type %#x\n", trans->type);
  255. return -EPROTO;
  256. }
  257. }
  258. /*
  259. * Wait for up to 6s til status register bit(s) turn 1 (in case wait_til_set
  260. * below is true) or 0. In case the wait was for the bit(s) to set - write
  261. * those bits back, which would cause resetting them.
  262. *
  263. * Return the last read status value on success or -1 on failure.
  264. */
  265. static int ich_status_poll(struct ich_spi_priv *ctlr, u16 bitmask,
  266. int wait_til_set)
  267. {
  268. int timeout = 600000; /* This will result in 6s */
  269. u16 status = 0;
  270. while (timeout--) {
  271. status = ich_readw(ctlr, ctlr->status);
  272. if (wait_til_set ^ ((status & bitmask) == 0)) {
  273. if (wait_til_set) {
  274. ich_writew(ctlr, status & bitmask,
  275. ctlr->status);
  276. }
  277. return status;
  278. }
  279. udelay(10);
  280. }
  281. printf("ICH SPI: SCIP timeout, read %x, expected %x\n",
  282. status, bitmask);
  283. return -ETIMEDOUT;
  284. }
  285. void ich_spi_config_opcode(struct udevice *dev)
  286. {
  287. struct ich_spi_priv *ctlr = dev_get_priv(dev);
  288. /*
  289. * PREOP, OPTYPE, OPMENU1/OPMENU2 registers can be locked down
  290. * to prevent accidental or intentional writes. Before they get
  291. * locked down, these registers should be initialized properly.
  292. */
  293. ich_writew(ctlr, SPI_OPPREFIX, ctlr->preop);
  294. ich_writew(ctlr, SPI_OPTYPE, ctlr->optype);
  295. ich_writel(ctlr, SPI_OPMENU_LOWER, ctlr->opmenu);
  296. ich_writel(ctlr, SPI_OPMENU_UPPER, ctlr->opmenu + sizeof(u32));
  297. }
  298. static int ich_spi_xfer(struct udevice *dev, unsigned int bitlen,
  299. const void *dout, void *din, unsigned long flags)
  300. {
  301. struct udevice *bus = dev_get_parent(dev);
  302. struct ich_spi_platdata *plat = dev_get_platdata(bus);
  303. struct ich_spi_priv *ctlr = dev_get_priv(bus);
  304. uint16_t control;
  305. int16_t opcode_index;
  306. int with_address;
  307. int status;
  308. int bytes = bitlen / 8;
  309. struct spi_trans *trans = &ctlr->trans;
  310. unsigned type = flags & (SPI_XFER_BEGIN | SPI_XFER_END);
  311. int using_cmd = 0;
  312. bool lock = spi_lock_status(plat, ctlr->base);
  313. int ret;
  314. /* We don't support writing partial bytes */
  315. if (bitlen % 8) {
  316. debug("ICH SPI: Accessing partial bytes not supported\n");
  317. return -EPROTONOSUPPORT;
  318. }
  319. /* An empty end transaction can be ignored */
  320. if (type == SPI_XFER_END && !dout && !din)
  321. return 0;
  322. if (type & SPI_XFER_BEGIN)
  323. memset(trans, '\0', sizeof(*trans));
  324. /* Dp we need to come back later to finish it? */
  325. if (dout && type == SPI_XFER_BEGIN) {
  326. if (bytes > ICH_MAX_CMD_LEN) {
  327. debug("ICH SPI: Command length limit exceeded\n");
  328. return -ENOSPC;
  329. }
  330. memcpy(trans->cmd, dout, bytes);
  331. trans->cmd_len = bytes;
  332. debug_trace("ICH SPI: Saved %d bytes\n", bytes);
  333. return 0;
  334. }
  335. /*
  336. * We process a 'middle' spi_xfer() call, which has no
  337. * SPI_XFER_BEGIN/END, as an independent transaction as if it had
  338. * an end. We therefore repeat the command. This is because ICH
  339. * seems to have no support for this, or because interest (in digging
  340. * out the details and creating a special case in the code) is low.
  341. */
  342. if (trans->cmd_len) {
  343. trans->out = trans->cmd;
  344. trans->bytesout = trans->cmd_len;
  345. using_cmd = 1;
  346. debug_trace("ICH SPI: Using %d bytes\n", trans->cmd_len);
  347. } else {
  348. trans->out = dout;
  349. trans->bytesout = dout ? bytes : 0;
  350. }
  351. trans->in = din;
  352. trans->bytesin = din ? bytes : 0;
  353. /* There has to always at least be an opcode */
  354. if (!trans->bytesout) {
  355. debug("ICH SPI: No opcode for transfer\n");
  356. return -EPROTO;
  357. }
  358. ret = ich_status_poll(ctlr, SPIS_SCIP, 0);
  359. if (ret < 0)
  360. return ret;
  361. if (plat->ich_version == ICHV_7)
  362. ich_writew(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
  363. else
  364. ich_writeb(ctlr, SPIS_CDS | SPIS_FCERR, ctlr->status);
  365. spi_setup_type(trans, using_cmd ? bytes : 0);
  366. opcode_index = spi_setup_opcode(ctlr, trans, lock);
  367. if (opcode_index < 0)
  368. return -EINVAL;
  369. with_address = spi_setup_offset(trans);
  370. if (with_address < 0)
  371. return -EINVAL;
  372. if (trans->opcode == SPI_OPCODE_WREN) {
  373. /*
  374. * Treat Write Enable as Atomic Pre-Op if possible
  375. * in order to prevent the Management Engine from
  376. * issuing a transaction between WREN and DATA.
  377. */
  378. if (!lock)
  379. ich_writew(ctlr, trans->opcode, ctlr->preop);
  380. return 0;
  381. }
  382. if (ctlr->speed && ctlr->max_speed >= 33000000) {
  383. int byte;
  384. byte = ich_readb(ctlr, ctlr->speed);
  385. if (ctlr->cur_speed >= 33000000)
  386. byte |= SSFC_SCF_33MHZ;
  387. else
  388. byte &= ~SSFC_SCF_33MHZ;
  389. ich_writeb(ctlr, byte, ctlr->speed);
  390. }
  391. /* See if we have used up the command data */
  392. if (using_cmd && dout && bytes) {
  393. trans->out = dout;
  394. trans->bytesout = bytes;
  395. debug_trace("ICH SPI: Moving to data, %d bytes\n", bytes);
  396. }
  397. /* Preset control fields */
  398. control = ich_readw(ctlr, ctlr->control);
  399. control &= ~SSFC_RESERVED;
  400. control = SPIC_SCGO | ((opcode_index & 0x07) << 4);
  401. /* Issue atomic preop cycle if needed */
  402. if (ich_readw(ctlr, ctlr->preop))
  403. control |= SPIC_ACS;
  404. if (!trans->bytesout && !trans->bytesin) {
  405. /* SPI addresses are 24 bit only */
  406. if (with_address) {
  407. ich_writel(ctlr, trans->offset & 0x00FFFFFF,
  408. ctlr->addr);
  409. }
  410. /*
  411. * This is a 'no data' command (like Write Enable), its
  412. * bitesout size was 1, decremented to zero while executing
  413. * spi_setup_opcode() above. Tell the chip to send the
  414. * command.
  415. */
  416. ich_writew(ctlr, control, ctlr->control);
  417. /* wait for the result */
  418. status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
  419. if (status < 0)
  420. return status;
  421. if (status & SPIS_FCERR) {
  422. debug("ICH SPI: Command transaction error\n");
  423. return -EIO;
  424. }
  425. return 0;
  426. }
  427. /*
  428. * Check if this is a write command atempting to transfer more bytes
  429. * than the controller can handle. Iterations for writes are not
  430. * supported here because each SPI write command needs to be preceded
  431. * and followed by other SPI commands, and this sequence is controlled
  432. * by the SPI chip driver.
  433. */
  434. if (trans->bytesout > ctlr->databytes) {
  435. debug("ICH SPI: Too much to write. This should be prevented by the driver's max_write_size?\n");
  436. return -EPROTO;
  437. }
  438. /*
  439. * Read or write up to databytes bytes at a time until everything has
  440. * been sent.
  441. */
  442. while (trans->bytesout || trans->bytesin) {
  443. uint32_t data_length;
  444. /* SPI addresses are 24 bit only */
  445. ich_writel(ctlr, trans->offset & 0x00FFFFFF, ctlr->addr);
  446. if (trans->bytesout)
  447. data_length = min(trans->bytesout, ctlr->databytes);
  448. else
  449. data_length = min(trans->bytesin, ctlr->databytes);
  450. /* Program data into FDATA0 to N */
  451. if (trans->bytesout) {
  452. write_reg(ctlr, trans->out, ctlr->data, data_length);
  453. spi_use_out(trans, data_length);
  454. if (with_address)
  455. trans->offset += data_length;
  456. }
  457. /* Add proper control fields' values */
  458. control &= ~((ctlr->databytes - 1) << 8);
  459. control |= SPIC_DS;
  460. control |= (data_length - 1) << 8;
  461. /* write it */
  462. ich_writew(ctlr, control, ctlr->control);
  463. /* Wait for Cycle Done Status or Flash Cycle Error */
  464. status = ich_status_poll(ctlr, SPIS_CDS | SPIS_FCERR, 1);
  465. if (status < 0)
  466. return status;
  467. if (status & SPIS_FCERR) {
  468. debug("ICH SPI: Data transaction error %x\n", status);
  469. return -EIO;
  470. }
  471. if (trans->bytesin) {
  472. read_reg(ctlr, ctlr->data, trans->in, data_length);
  473. spi_use_in(trans, data_length);
  474. if (with_address)
  475. trans->offset += data_length;
  476. }
  477. }
  478. /* Clear atomic preop now that xfer is done */
  479. ich_writew(ctlr, 0, ctlr->preop);
  480. return 0;
  481. }
  482. static int ich_spi_probe(struct udevice *dev)
  483. {
  484. struct ich_spi_platdata *plat = dev_get_platdata(dev);
  485. struct ich_spi_priv *priv = dev_get_priv(dev);
  486. uint8_t bios_cntl;
  487. int ret;
  488. ret = ich_init_controller(dev, plat, priv);
  489. if (ret)
  490. return ret;
  491. /* Disable the BIOS write protect so write commands are allowed */
  492. ret = pch_set_spi_protect(dev->parent, false);
  493. if (ret == -ENOSYS) {
  494. bios_cntl = ich_readb(priv, priv->bcr);
  495. bios_cntl &= ~BIT(5); /* clear Enable InSMM_STS (EISS) */
  496. bios_cntl |= 1; /* Write Protect Disable (WPD) */
  497. ich_writeb(priv, bios_cntl, priv->bcr);
  498. } else if (ret) {
  499. debug("%s: Failed to disable write-protect: err=%d\n",
  500. __func__, ret);
  501. return ret;
  502. }
  503. priv->cur_speed = priv->max_speed;
  504. return 0;
  505. }
  506. static int ich_spi_remove(struct udevice *bus)
  507. {
  508. /*
  509. * Configure SPI controller so that the Linux MTD driver can fully
  510. * access the SPI NOR chip
  511. */
  512. ich_spi_config_opcode(bus);
  513. return 0;
  514. }
  515. static int ich_spi_set_speed(struct udevice *bus, uint speed)
  516. {
  517. struct ich_spi_priv *priv = dev_get_priv(bus);
  518. priv->cur_speed = speed;
  519. return 0;
  520. }
  521. static int ich_spi_set_mode(struct udevice *bus, uint mode)
  522. {
  523. debug("%s: mode=%d\n", __func__, mode);
  524. return 0;
  525. }
  526. static int ich_spi_child_pre_probe(struct udevice *dev)
  527. {
  528. struct udevice *bus = dev_get_parent(dev);
  529. struct ich_spi_platdata *plat = dev_get_platdata(bus);
  530. struct ich_spi_priv *priv = dev_get_priv(bus);
  531. struct spi_slave *slave = dev_get_parent_priv(dev);
  532. /*
  533. * Yes this controller can only write a small number of bytes at
  534. * once! The limit is typically 64 bytes.
  535. */
  536. slave->max_write_size = priv->databytes;
  537. /*
  538. * ICH 7 SPI controller only supports array read command
  539. * and byte program command for SST flash
  540. */
  541. if (plat->ich_version == ICHV_7)
  542. slave->mode = SPI_RX_SLOW | SPI_TX_BYTE;
  543. return 0;
  544. }
  545. static int ich_spi_ofdata_to_platdata(struct udevice *dev)
  546. {
  547. struct ich_spi_platdata *plat = dev_get_platdata(dev);
  548. int node = dev_of_offset(dev);
  549. int ret;
  550. ret = fdt_node_check_compatible(gd->fdt_blob, node, "intel,ich7-spi");
  551. if (ret == 0) {
  552. plat->ich_version = ICHV_7;
  553. } else {
  554. ret = fdt_node_check_compatible(gd->fdt_blob, node,
  555. "intel,ich9-spi");
  556. if (ret == 0)
  557. plat->ich_version = ICHV_9;
  558. }
  559. return ret;
  560. }
  561. static const struct dm_spi_ops ich_spi_ops = {
  562. .xfer = ich_spi_xfer,
  563. .set_speed = ich_spi_set_speed,
  564. .set_mode = ich_spi_set_mode,
  565. /*
  566. * cs_info is not needed, since we require all chip selects to be
  567. * in the device tree explicitly
  568. */
  569. };
  570. static const struct udevice_id ich_spi_ids[] = {
  571. { .compatible = "intel,ich7-spi" },
  572. { .compatible = "intel,ich9-spi" },
  573. { }
  574. };
  575. U_BOOT_DRIVER(ich_spi) = {
  576. .name = "ich_spi",
  577. .id = UCLASS_SPI,
  578. .of_match = ich_spi_ids,
  579. .ops = &ich_spi_ops,
  580. .ofdata_to_platdata = ich_spi_ofdata_to_platdata,
  581. .platdata_auto_alloc_size = sizeof(struct ich_spi_platdata),
  582. .priv_auto_alloc_size = sizeof(struct ich_spi_priv),
  583. .child_pre_probe = ich_spi_child_pre_probe,
  584. .probe = ich_spi_probe,
  585. .remove = ich_spi_remove,
  586. .flags = DM_FLAG_OS_PREPARE,
  587. };