ich.c 18 KB

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