ich.c 19 KB

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