ich.c 21 KB

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