ich.c 18 KB

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