ich.c 18 KB

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