ich.c 20 KB

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