intel-spi.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777
  1. /*
  2. * Intel PCH/PCU SPI flash driver.
  3. *
  4. * Copyright (C) 2016, Intel Corporation
  5. * Author: Mika Westerberg <mika.westerberg@linux.intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/iopoll.h>
  14. #include <linux/module.h>
  15. #include <linux/sched.h>
  16. #include <linux/sizes.h>
  17. #include <linux/mtd/mtd.h>
  18. #include <linux/mtd/partitions.h>
  19. #include <linux/mtd/spi-nor.h>
  20. #include <linux/platform_data/intel-spi.h>
  21. #include "intel-spi.h"
  22. /* Offsets are from @ispi->base */
  23. #define BFPREG 0x00
  24. #define HSFSTS_CTL 0x04
  25. #define HSFSTS_CTL_FSMIE BIT(31)
  26. #define HSFSTS_CTL_FDBC_SHIFT 24
  27. #define HSFSTS_CTL_FDBC_MASK (0x3f << HSFSTS_CTL_FDBC_SHIFT)
  28. #define HSFSTS_CTL_FCYCLE_SHIFT 17
  29. #define HSFSTS_CTL_FCYCLE_MASK (0x0f << HSFSTS_CTL_FCYCLE_SHIFT)
  30. /* HW sequencer opcodes */
  31. #define HSFSTS_CTL_FCYCLE_READ (0x00 << HSFSTS_CTL_FCYCLE_SHIFT)
  32. #define HSFSTS_CTL_FCYCLE_WRITE (0x02 << HSFSTS_CTL_FCYCLE_SHIFT)
  33. #define HSFSTS_CTL_FCYCLE_ERASE (0x03 << HSFSTS_CTL_FCYCLE_SHIFT)
  34. #define HSFSTS_CTL_FCYCLE_ERASE_64K (0x04 << HSFSTS_CTL_FCYCLE_SHIFT)
  35. #define HSFSTS_CTL_FCYCLE_RDID (0x06 << HSFSTS_CTL_FCYCLE_SHIFT)
  36. #define HSFSTS_CTL_FCYCLE_WRSR (0x07 << HSFSTS_CTL_FCYCLE_SHIFT)
  37. #define HSFSTS_CTL_FCYCLE_RDSR (0x08 << HSFSTS_CTL_FCYCLE_SHIFT)
  38. #define HSFSTS_CTL_FGO BIT(16)
  39. #define HSFSTS_CTL_FLOCKDN BIT(15)
  40. #define HSFSTS_CTL_FDV BIT(14)
  41. #define HSFSTS_CTL_SCIP BIT(5)
  42. #define HSFSTS_CTL_AEL BIT(2)
  43. #define HSFSTS_CTL_FCERR BIT(1)
  44. #define HSFSTS_CTL_FDONE BIT(0)
  45. #define FADDR 0x08
  46. #define DLOCK 0x0c
  47. #define FDATA(n) (0x10 + ((n) * 4))
  48. #define FRACC 0x50
  49. #define FREG(n) (0x54 + ((n) * 4))
  50. #define FREG_BASE_MASK 0x3fff
  51. #define FREG_LIMIT_SHIFT 16
  52. #define FREG_LIMIT_MASK (0x03fff << FREG_LIMIT_SHIFT)
  53. /* Offset is from @ispi->pregs */
  54. #define PR(n) ((n) * 4)
  55. #define PR_WPE BIT(31)
  56. #define PR_LIMIT_SHIFT 16
  57. #define PR_LIMIT_MASK (0x3fff << PR_LIMIT_SHIFT)
  58. #define PR_RPE BIT(15)
  59. #define PR_BASE_MASK 0x3fff
  60. /* Last PR is GPR0 */
  61. #define PR_NUM (5 + 1)
  62. /* Offsets are from @ispi->sregs */
  63. #define SSFSTS_CTL 0x00
  64. #define SSFSTS_CTL_FSMIE BIT(23)
  65. #define SSFSTS_CTL_DS BIT(22)
  66. #define SSFSTS_CTL_DBC_SHIFT 16
  67. #define SSFSTS_CTL_SPOP BIT(11)
  68. #define SSFSTS_CTL_ACS BIT(10)
  69. #define SSFSTS_CTL_SCGO BIT(9)
  70. #define SSFSTS_CTL_COP_SHIFT 12
  71. #define SSFSTS_CTL_FRS BIT(7)
  72. #define SSFSTS_CTL_DOFRS BIT(6)
  73. #define SSFSTS_CTL_AEL BIT(4)
  74. #define SSFSTS_CTL_FCERR BIT(3)
  75. #define SSFSTS_CTL_FDONE BIT(2)
  76. #define SSFSTS_CTL_SCIP BIT(0)
  77. #define PREOP_OPTYPE 0x04
  78. #define OPMENU0 0x08
  79. #define OPMENU1 0x0c
  80. /* CPU specifics */
  81. #define BYT_PR 0x74
  82. #define BYT_SSFSTS_CTL 0x90
  83. #define BYT_BCR 0xfc
  84. #define BYT_BCR_WPD BIT(0)
  85. #define BYT_FREG_NUM 5
  86. #define LPT_PR 0x74
  87. #define LPT_SSFSTS_CTL 0x90
  88. #define LPT_FREG_NUM 5
  89. #define BXT_PR 0x84
  90. #define BXT_SSFSTS_CTL 0xa0
  91. #define BXT_FREG_NUM 12
  92. #define INTEL_SPI_TIMEOUT 5000 /* ms */
  93. #define INTEL_SPI_FIFO_SZ 64
  94. /**
  95. * struct intel_spi - Driver private data
  96. * @dev: Device pointer
  97. * @info: Pointer to board specific info
  98. * @nor: SPI NOR layer structure
  99. * @base: Beginning of MMIO space
  100. * @pregs: Start of protection registers
  101. * @sregs: Start of software sequencer registers
  102. * @nregions: Maximum number of regions
  103. * @writeable: Is the chip writeable
  104. * @swseq: Use SW sequencer in register reads/writes
  105. * @erase_64k: 64k erase supported
  106. * @opcodes: Opcodes which are supported. This are programmed by BIOS
  107. * before it locks down the controller.
  108. * @preopcodes: Preopcodes which are supported.
  109. */
  110. struct intel_spi {
  111. struct device *dev;
  112. const struct intel_spi_boardinfo *info;
  113. struct spi_nor nor;
  114. void __iomem *base;
  115. void __iomem *pregs;
  116. void __iomem *sregs;
  117. size_t nregions;
  118. bool writeable;
  119. bool swseq;
  120. bool erase_64k;
  121. u8 opcodes[8];
  122. u8 preopcodes[2];
  123. };
  124. static bool writeable;
  125. module_param(writeable, bool, 0);
  126. MODULE_PARM_DESC(writeable, "Enable write access to SPI flash chip (default=0)");
  127. static void intel_spi_dump_regs(struct intel_spi *ispi)
  128. {
  129. u32 value;
  130. int i;
  131. dev_dbg(ispi->dev, "BFPREG=0x%08x\n", readl(ispi->base + BFPREG));
  132. value = readl(ispi->base + HSFSTS_CTL);
  133. dev_dbg(ispi->dev, "HSFSTS_CTL=0x%08x\n", value);
  134. if (value & HSFSTS_CTL_FLOCKDN)
  135. dev_dbg(ispi->dev, "-> Locked\n");
  136. dev_dbg(ispi->dev, "FADDR=0x%08x\n", readl(ispi->base + FADDR));
  137. dev_dbg(ispi->dev, "DLOCK=0x%08x\n", readl(ispi->base + DLOCK));
  138. for (i = 0; i < 16; i++)
  139. dev_dbg(ispi->dev, "FDATA(%d)=0x%08x\n",
  140. i, readl(ispi->base + FDATA(i)));
  141. dev_dbg(ispi->dev, "FRACC=0x%08x\n", readl(ispi->base + FRACC));
  142. for (i = 0; i < ispi->nregions; i++)
  143. dev_dbg(ispi->dev, "FREG(%d)=0x%08x\n", i,
  144. readl(ispi->base + FREG(i)));
  145. for (i = 0; i < PR_NUM; i++)
  146. dev_dbg(ispi->dev, "PR(%d)=0x%08x\n", i,
  147. readl(ispi->pregs + PR(i)));
  148. value = readl(ispi->sregs + SSFSTS_CTL);
  149. dev_dbg(ispi->dev, "SSFSTS_CTL=0x%08x\n", value);
  150. dev_dbg(ispi->dev, "PREOP_OPTYPE=0x%08x\n",
  151. readl(ispi->sregs + PREOP_OPTYPE));
  152. dev_dbg(ispi->dev, "OPMENU0=0x%08x\n", readl(ispi->sregs + OPMENU0));
  153. dev_dbg(ispi->dev, "OPMENU1=0x%08x\n", readl(ispi->sregs + OPMENU1));
  154. if (ispi->info->type == INTEL_SPI_BYT)
  155. dev_dbg(ispi->dev, "BCR=0x%08x\n", readl(ispi->base + BYT_BCR));
  156. dev_dbg(ispi->dev, "Protected regions:\n");
  157. for (i = 0; i < PR_NUM; i++) {
  158. u32 base, limit;
  159. value = readl(ispi->pregs + PR(i));
  160. if (!(value & (PR_WPE | PR_RPE)))
  161. continue;
  162. limit = (value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
  163. base = value & PR_BASE_MASK;
  164. dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
  165. i, base << 12, (limit << 12) | 0xfff,
  166. value & PR_WPE ? 'W' : '.',
  167. value & PR_RPE ? 'R' : '.');
  168. }
  169. dev_dbg(ispi->dev, "Flash regions:\n");
  170. for (i = 0; i < ispi->nregions; i++) {
  171. u32 region, base, limit;
  172. region = readl(ispi->base + FREG(i));
  173. base = region & FREG_BASE_MASK;
  174. limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
  175. if (base >= limit || (i > 0 && limit == 0))
  176. dev_dbg(ispi->dev, " %02d disabled\n", i);
  177. else
  178. dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x\n",
  179. i, base << 12, (limit << 12) | 0xfff);
  180. }
  181. dev_dbg(ispi->dev, "Using %cW sequencer for register access\n",
  182. ispi->swseq ? 'S' : 'H');
  183. }
  184. /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
  185. static int intel_spi_read_block(struct intel_spi *ispi, void *buf, size_t size)
  186. {
  187. size_t bytes;
  188. int i = 0;
  189. if (size > INTEL_SPI_FIFO_SZ)
  190. return -EINVAL;
  191. while (size > 0) {
  192. bytes = min_t(size_t, size, 4);
  193. memcpy_fromio(buf, ispi->base + FDATA(i), bytes);
  194. size -= bytes;
  195. buf += bytes;
  196. i++;
  197. }
  198. return 0;
  199. }
  200. /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
  201. static int intel_spi_write_block(struct intel_spi *ispi, const void *buf,
  202. size_t size)
  203. {
  204. size_t bytes;
  205. int i = 0;
  206. if (size > INTEL_SPI_FIFO_SZ)
  207. return -EINVAL;
  208. while (size > 0) {
  209. bytes = min_t(size_t, size, 4);
  210. memcpy_toio(ispi->base + FDATA(i), buf, bytes);
  211. size -= bytes;
  212. buf += bytes;
  213. i++;
  214. }
  215. return 0;
  216. }
  217. static int intel_spi_wait_hw_busy(struct intel_spi *ispi)
  218. {
  219. u32 val;
  220. return readl_poll_timeout(ispi->base + HSFSTS_CTL, val,
  221. !(val & HSFSTS_CTL_SCIP), 0,
  222. INTEL_SPI_TIMEOUT * 1000);
  223. }
  224. static int intel_spi_wait_sw_busy(struct intel_spi *ispi)
  225. {
  226. u32 val;
  227. return readl_poll_timeout(ispi->sregs + SSFSTS_CTL, val,
  228. !(val & SSFSTS_CTL_SCIP), 0,
  229. INTEL_SPI_TIMEOUT * 1000);
  230. }
  231. static int intel_spi_init(struct intel_spi *ispi)
  232. {
  233. u32 opmenu0, opmenu1, val;
  234. int i;
  235. switch (ispi->info->type) {
  236. case INTEL_SPI_BYT:
  237. ispi->sregs = ispi->base + BYT_SSFSTS_CTL;
  238. ispi->pregs = ispi->base + BYT_PR;
  239. ispi->nregions = BYT_FREG_NUM;
  240. if (writeable) {
  241. /* Disable write protection */
  242. val = readl(ispi->base + BYT_BCR);
  243. if (!(val & BYT_BCR_WPD)) {
  244. val |= BYT_BCR_WPD;
  245. writel(val, ispi->base + BYT_BCR);
  246. val = readl(ispi->base + BYT_BCR);
  247. }
  248. ispi->writeable = !!(val & BYT_BCR_WPD);
  249. }
  250. break;
  251. case INTEL_SPI_LPT:
  252. ispi->sregs = ispi->base + LPT_SSFSTS_CTL;
  253. ispi->pregs = ispi->base + LPT_PR;
  254. ispi->nregions = LPT_FREG_NUM;
  255. break;
  256. case INTEL_SPI_BXT:
  257. ispi->sregs = ispi->base + BXT_SSFSTS_CTL;
  258. ispi->pregs = ispi->base + BXT_PR;
  259. ispi->nregions = BXT_FREG_NUM;
  260. ispi->erase_64k = true;
  261. break;
  262. default:
  263. return -EINVAL;
  264. }
  265. /* Disable #SMI generation */
  266. val = readl(ispi->base + HSFSTS_CTL);
  267. val &= ~HSFSTS_CTL_FSMIE;
  268. writel(val, ispi->base + HSFSTS_CTL);
  269. /*
  270. * BIOS programs allowed opcodes and then locks down the register.
  271. * So read back what opcodes it decided to support. That's the set
  272. * we are going to support as well.
  273. */
  274. opmenu0 = readl(ispi->sregs + OPMENU0);
  275. opmenu1 = readl(ispi->sregs + OPMENU1);
  276. /*
  277. * Some controllers can only do basic operations using hardware
  278. * sequencer. All other operations are supposed to be carried out
  279. * using software sequencer. If we find that BIOS has programmed
  280. * opcodes for the software sequencer we use that over the hardware
  281. * sequencer.
  282. */
  283. if (opmenu0 && opmenu1) {
  284. for (i = 0; i < ARRAY_SIZE(ispi->opcodes) / 2; i++) {
  285. ispi->opcodes[i] = opmenu0 >> i * 8;
  286. ispi->opcodes[i + 4] = opmenu1 >> i * 8;
  287. }
  288. val = readl(ispi->sregs + PREOP_OPTYPE);
  289. ispi->preopcodes[0] = val;
  290. ispi->preopcodes[1] = val >> 8;
  291. /* Disable #SMI generation from SW sequencer */
  292. val = readl(ispi->sregs + SSFSTS_CTL);
  293. val &= ~SSFSTS_CTL_FSMIE;
  294. writel(val, ispi->sregs + SSFSTS_CTL);
  295. ispi->swseq = true;
  296. }
  297. intel_spi_dump_regs(ispi);
  298. return 0;
  299. }
  300. static int intel_spi_opcode_index(struct intel_spi *ispi, u8 opcode)
  301. {
  302. int i;
  303. for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++)
  304. if (ispi->opcodes[i] == opcode)
  305. return i;
  306. return -EINVAL;
  307. }
  308. static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, u8 *buf,
  309. int len)
  310. {
  311. u32 val, status;
  312. int ret;
  313. val = readl(ispi->base + HSFSTS_CTL);
  314. val &= ~(HSFSTS_CTL_FCYCLE_MASK | HSFSTS_CTL_FDBC_MASK);
  315. switch (opcode) {
  316. case SPINOR_OP_RDID:
  317. val |= HSFSTS_CTL_FCYCLE_RDID;
  318. break;
  319. case SPINOR_OP_WRSR:
  320. val |= HSFSTS_CTL_FCYCLE_WRSR;
  321. break;
  322. case SPINOR_OP_RDSR:
  323. val |= HSFSTS_CTL_FCYCLE_RDSR;
  324. break;
  325. default:
  326. return -EINVAL;
  327. }
  328. val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
  329. val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  330. val |= HSFSTS_CTL_FGO;
  331. writel(val, ispi->base + HSFSTS_CTL);
  332. ret = intel_spi_wait_hw_busy(ispi);
  333. if (ret)
  334. return ret;
  335. status = readl(ispi->base + HSFSTS_CTL);
  336. if (status & HSFSTS_CTL_FCERR)
  337. return -EIO;
  338. else if (status & HSFSTS_CTL_AEL)
  339. return -EACCES;
  340. return 0;
  341. }
  342. static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, u8 *buf,
  343. int len)
  344. {
  345. u32 val, status;
  346. int ret;
  347. ret = intel_spi_opcode_index(ispi, opcode);
  348. if (ret < 0)
  349. return ret;
  350. val = (len << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
  351. val |= ret << SSFSTS_CTL_COP_SHIFT;
  352. val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
  353. val |= SSFSTS_CTL_SCGO;
  354. writel(val, ispi->sregs + SSFSTS_CTL);
  355. ret = intel_spi_wait_sw_busy(ispi);
  356. if (ret)
  357. return ret;
  358. status = readl(ispi->base + SSFSTS_CTL);
  359. if (status & SSFSTS_CTL_FCERR)
  360. return -EIO;
  361. else if (status & SSFSTS_CTL_AEL)
  362. return -EACCES;
  363. return 0;
  364. }
  365. static int intel_spi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  366. {
  367. struct intel_spi *ispi = nor->priv;
  368. int ret;
  369. /* Address of the first chip */
  370. writel(0, ispi->base + FADDR);
  371. if (ispi->swseq)
  372. ret = intel_spi_sw_cycle(ispi, opcode, buf, len);
  373. else
  374. ret = intel_spi_hw_cycle(ispi, opcode, buf, len);
  375. if (ret)
  376. return ret;
  377. return intel_spi_read_block(ispi, buf, len);
  378. }
  379. static int intel_spi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  380. {
  381. struct intel_spi *ispi = nor->priv;
  382. int ret;
  383. /*
  384. * This is handled with atomic operation and preop code in Intel
  385. * controller so skip it here now.
  386. */
  387. if (opcode == SPINOR_OP_WREN)
  388. return 0;
  389. writel(0, ispi->base + FADDR);
  390. /* Write the value beforehand */
  391. ret = intel_spi_write_block(ispi, buf, len);
  392. if (ret)
  393. return ret;
  394. if (ispi->swseq)
  395. return intel_spi_sw_cycle(ispi, opcode, buf, len);
  396. return intel_spi_hw_cycle(ispi, opcode, buf, len);
  397. }
  398. static ssize_t intel_spi_read(struct spi_nor *nor, loff_t from, size_t len,
  399. u_char *read_buf)
  400. {
  401. struct intel_spi *ispi = nor->priv;
  402. size_t block_size, retlen = 0;
  403. u32 val, status;
  404. ssize_t ret;
  405. switch (nor->read_opcode) {
  406. case SPINOR_OP_READ:
  407. case SPINOR_OP_READ_FAST:
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. while (len > 0) {
  413. block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
  414. writel(from, ispi->base + FADDR);
  415. val = readl(ispi->base + HSFSTS_CTL);
  416. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  417. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  418. val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
  419. val |= HSFSTS_CTL_FCYCLE_READ;
  420. val |= HSFSTS_CTL_FGO;
  421. writel(val, ispi->base + HSFSTS_CTL);
  422. ret = intel_spi_wait_hw_busy(ispi);
  423. if (ret)
  424. return ret;
  425. status = readl(ispi->base + HSFSTS_CTL);
  426. if (status & HSFSTS_CTL_FCERR)
  427. ret = -EIO;
  428. else if (status & HSFSTS_CTL_AEL)
  429. ret = -EACCES;
  430. if (ret < 0) {
  431. dev_err(ispi->dev, "read error: %llx: %#x\n", from,
  432. status);
  433. return ret;
  434. }
  435. ret = intel_spi_read_block(ispi, read_buf, block_size);
  436. if (ret)
  437. return ret;
  438. len -= block_size;
  439. from += block_size;
  440. retlen += block_size;
  441. read_buf += block_size;
  442. }
  443. return retlen;
  444. }
  445. static ssize_t intel_spi_write(struct spi_nor *nor, loff_t to, size_t len,
  446. const u_char *write_buf)
  447. {
  448. struct intel_spi *ispi = nor->priv;
  449. size_t block_size, retlen = 0;
  450. u32 val, status;
  451. ssize_t ret;
  452. while (len > 0) {
  453. block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
  454. writel(to, ispi->base + FADDR);
  455. val = readl(ispi->base + HSFSTS_CTL);
  456. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  457. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  458. val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
  459. val |= HSFSTS_CTL_FCYCLE_WRITE;
  460. /* Write enable */
  461. if (ispi->preopcodes[1] == SPINOR_OP_WREN)
  462. val |= SSFSTS_CTL_SPOP;
  463. val |= SSFSTS_CTL_ACS;
  464. writel(val, ispi->base + HSFSTS_CTL);
  465. ret = intel_spi_write_block(ispi, write_buf, block_size);
  466. if (ret) {
  467. dev_err(ispi->dev, "failed to write block\n");
  468. return ret;
  469. }
  470. /* Start the write now */
  471. val = readl(ispi->base + HSFSTS_CTL);
  472. writel(val | HSFSTS_CTL_FGO, ispi->base + HSFSTS_CTL);
  473. ret = intel_spi_wait_hw_busy(ispi);
  474. if (ret) {
  475. dev_err(ispi->dev, "timeout\n");
  476. return ret;
  477. }
  478. status = readl(ispi->base + HSFSTS_CTL);
  479. if (status & HSFSTS_CTL_FCERR)
  480. ret = -EIO;
  481. else if (status & HSFSTS_CTL_AEL)
  482. ret = -EACCES;
  483. if (ret < 0) {
  484. dev_err(ispi->dev, "write error: %llx: %#x\n", to,
  485. status);
  486. return ret;
  487. }
  488. len -= block_size;
  489. to += block_size;
  490. retlen += block_size;
  491. write_buf += block_size;
  492. }
  493. return retlen;
  494. }
  495. static int intel_spi_erase(struct spi_nor *nor, loff_t offs)
  496. {
  497. size_t erase_size, len = nor->mtd.erasesize;
  498. struct intel_spi *ispi = nor->priv;
  499. u32 val, status, cmd;
  500. int ret;
  501. /* If the hardware can do 64k erase use that when possible */
  502. if (len >= SZ_64K && ispi->erase_64k) {
  503. cmd = HSFSTS_CTL_FCYCLE_ERASE_64K;
  504. erase_size = SZ_64K;
  505. } else {
  506. cmd = HSFSTS_CTL_FCYCLE_ERASE;
  507. erase_size = SZ_4K;
  508. }
  509. while (len > 0) {
  510. writel(offs, ispi->base + FADDR);
  511. val = readl(ispi->base + HSFSTS_CTL);
  512. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  513. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  514. val |= cmd;
  515. val |= HSFSTS_CTL_FGO;
  516. writel(val, ispi->base + HSFSTS_CTL);
  517. ret = intel_spi_wait_hw_busy(ispi);
  518. if (ret)
  519. return ret;
  520. status = readl(ispi->base + HSFSTS_CTL);
  521. if (status & HSFSTS_CTL_FCERR)
  522. return -EIO;
  523. else if (status & HSFSTS_CTL_AEL)
  524. return -EACCES;
  525. offs += erase_size;
  526. len -= erase_size;
  527. }
  528. return 0;
  529. }
  530. static bool intel_spi_is_protected(const struct intel_spi *ispi,
  531. unsigned int base, unsigned int limit)
  532. {
  533. int i;
  534. for (i = 0; i < PR_NUM; i++) {
  535. u32 pr_base, pr_limit, pr_value;
  536. pr_value = readl(ispi->pregs + PR(i));
  537. if (!(pr_value & (PR_WPE | PR_RPE)))
  538. continue;
  539. pr_limit = (pr_value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
  540. pr_base = pr_value & PR_BASE_MASK;
  541. if (pr_base >= base && pr_limit <= limit)
  542. return true;
  543. }
  544. return false;
  545. }
  546. /*
  547. * There will be a single partition holding all enabled flash regions. We
  548. * call this "BIOS".
  549. */
  550. static void intel_spi_fill_partition(struct intel_spi *ispi,
  551. struct mtd_partition *part)
  552. {
  553. u64 end;
  554. int i;
  555. memset(part, 0, sizeof(*part));
  556. /* Start from the mandatory descriptor region */
  557. part->size = 4096;
  558. part->name = "BIOS";
  559. /*
  560. * Now try to find where this partition ends based on the flash
  561. * region registers.
  562. */
  563. for (i = 1; i < ispi->nregions; i++) {
  564. u32 region, base, limit;
  565. region = readl(ispi->base + FREG(i));
  566. base = region & FREG_BASE_MASK;
  567. limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
  568. if (base >= limit || limit == 0)
  569. continue;
  570. /*
  571. * If any of the regions have protection bits set, make the
  572. * whole partition read-only to be on the safe side.
  573. */
  574. if (intel_spi_is_protected(ispi, base, limit))
  575. ispi->writeable = 0;
  576. end = (limit << 12) + 4096;
  577. if (end > part->size)
  578. part->size = end;
  579. }
  580. }
  581. struct intel_spi *intel_spi_probe(struct device *dev,
  582. struct resource *mem, const struct intel_spi_boardinfo *info)
  583. {
  584. struct mtd_partition part;
  585. struct intel_spi *ispi;
  586. int ret;
  587. if (!info || !mem)
  588. return ERR_PTR(-EINVAL);
  589. ispi = devm_kzalloc(dev, sizeof(*ispi), GFP_KERNEL);
  590. if (!ispi)
  591. return ERR_PTR(-ENOMEM);
  592. ispi->base = devm_ioremap_resource(dev, mem);
  593. if (IS_ERR(ispi->base))
  594. return ispi->base;
  595. ispi->dev = dev;
  596. ispi->info = info;
  597. ispi->writeable = info->writeable;
  598. ret = intel_spi_init(ispi);
  599. if (ret)
  600. return ERR_PTR(ret);
  601. ispi->nor.dev = ispi->dev;
  602. ispi->nor.priv = ispi;
  603. ispi->nor.read_reg = intel_spi_read_reg;
  604. ispi->nor.write_reg = intel_spi_write_reg;
  605. ispi->nor.read = intel_spi_read;
  606. ispi->nor.write = intel_spi_write;
  607. ispi->nor.erase = intel_spi_erase;
  608. ret = spi_nor_scan(&ispi->nor, NULL, SPI_NOR_NORMAL);
  609. if (ret) {
  610. dev_info(dev, "failed to locate the chip\n");
  611. return ERR_PTR(ret);
  612. }
  613. intel_spi_fill_partition(ispi, &part);
  614. /* Prevent writes if not explicitly enabled */
  615. if (!ispi->writeable || !writeable)
  616. ispi->nor.mtd.flags &= ~MTD_WRITEABLE;
  617. ret = mtd_device_parse_register(&ispi->nor.mtd, NULL, NULL, &part, 1);
  618. if (ret)
  619. return ERR_PTR(ret);
  620. return ispi;
  621. }
  622. EXPORT_SYMBOL_GPL(intel_spi_probe);
  623. int intel_spi_remove(struct intel_spi *ispi)
  624. {
  625. return mtd_device_unregister(&ispi->nor.mtd);
  626. }
  627. EXPORT_SYMBOL_GPL(intel_spi_remove);
  628. MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
  629. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  630. MODULE_LICENSE("GPL v2");