ich.c 19 KB

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