ich.c 21 KB

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