intel-spi.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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. /* Offsets are from @ispi->sregs */
  61. #define SSFSTS_CTL 0x00
  62. #define SSFSTS_CTL_FSMIE BIT(23)
  63. #define SSFSTS_CTL_DS BIT(22)
  64. #define SSFSTS_CTL_DBC_SHIFT 16
  65. #define SSFSTS_CTL_SPOP BIT(11)
  66. #define SSFSTS_CTL_ACS BIT(10)
  67. #define SSFSTS_CTL_SCGO BIT(9)
  68. #define SSFSTS_CTL_COP_SHIFT 12
  69. #define SSFSTS_CTL_FRS BIT(7)
  70. #define SSFSTS_CTL_DOFRS BIT(6)
  71. #define SSFSTS_CTL_AEL BIT(4)
  72. #define SSFSTS_CTL_FCERR BIT(3)
  73. #define SSFSTS_CTL_FDONE BIT(2)
  74. #define SSFSTS_CTL_SCIP BIT(0)
  75. #define PREOP_OPTYPE 0x04
  76. #define OPMENU0 0x08
  77. #define OPMENU1 0x0c
  78. #define OPTYPE_READ_NO_ADDR 0
  79. #define OPTYPE_WRITE_NO_ADDR 1
  80. #define OPTYPE_READ_WITH_ADDR 2
  81. #define OPTYPE_WRITE_WITH_ADDR 3
  82. /* CPU specifics */
  83. #define BYT_PR 0x74
  84. #define BYT_SSFSTS_CTL 0x90
  85. #define BYT_BCR 0xfc
  86. #define BYT_BCR_WPD BIT(0)
  87. #define BYT_FREG_NUM 5
  88. #define BYT_PR_NUM 5
  89. #define LPT_PR 0x74
  90. #define LPT_SSFSTS_CTL 0x90
  91. #define LPT_FREG_NUM 5
  92. #define LPT_PR_NUM 5
  93. #define BXT_PR 0x84
  94. #define BXT_SSFSTS_CTL 0xa0
  95. #define BXT_FREG_NUM 12
  96. #define BXT_PR_NUM 6
  97. #define LVSCC 0xc4
  98. #define UVSCC 0xc8
  99. #define ERASE_OPCODE_SHIFT 8
  100. #define ERASE_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
  101. #define ERASE_64K_OPCODE_SHIFT 16
  102. #define ERASE_64K_OPCODE_MASK (0xff << ERASE_OPCODE_SHIFT)
  103. #define INTEL_SPI_TIMEOUT 5000 /* ms */
  104. #define INTEL_SPI_FIFO_SZ 64
  105. /**
  106. * struct intel_spi - Driver private data
  107. * @dev: Device pointer
  108. * @info: Pointer to board specific info
  109. * @nor: SPI NOR layer structure
  110. * @base: Beginning of MMIO space
  111. * @pregs: Start of protection registers
  112. * @sregs: Start of software sequencer registers
  113. * @nregions: Maximum number of regions
  114. * @pr_num: Maximum number of protected range registers
  115. * @writeable: Is the chip writeable
  116. * @locked: Is SPI setting locked
  117. * @swseq_reg: Use SW sequencer in register reads/writes
  118. * @swseq_erase: Use SW sequencer in erase operation
  119. * @erase_64k: 64k erase supported
  120. * @atomic_preopcode: Holds preopcode when atomic sequence is requested
  121. * @opcodes: Opcodes which are supported. This are programmed by BIOS
  122. * before it locks down the controller.
  123. */
  124. struct intel_spi {
  125. struct device *dev;
  126. const struct intel_spi_boardinfo *info;
  127. struct spi_nor nor;
  128. void __iomem *base;
  129. void __iomem *pregs;
  130. void __iomem *sregs;
  131. size_t nregions;
  132. size_t pr_num;
  133. bool writeable;
  134. bool locked;
  135. bool swseq_reg;
  136. bool swseq_erase;
  137. bool erase_64k;
  138. u8 atomic_preopcode;
  139. u8 opcodes[8];
  140. };
  141. static bool writeable;
  142. module_param(writeable, bool, 0);
  143. MODULE_PARM_DESC(writeable, "Enable write access to SPI flash chip (default=0)");
  144. static void intel_spi_dump_regs(struct intel_spi *ispi)
  145. {
  146. u32 value;
  147. int i;
  148. dev_dbg(ispi->dev, "BFPREG=0x%08x\n", readl(ispi->base + BFPREG));
  149. value = readl(ispi->base + HSFSTS_CTL);
  150. dev_dbg(ispi->dev, "HSFSTS_CTL=0x%08x\n", value);
  151. if (value & HSFSTS_CTL_FLOCKDN)
  152. dev_dbg(ispi->dev, "-> Locked\n");
  153. dev_dbg(ispi->dev, "FADDR=0x%08x\n", readl(ispi->base + FADDR));
  154. dev_dbg(ispi->dev, "DLOCK=0x%08x\n", readl(ispi->base + DLOCK));
  155. for (i = 0; i < 16; i++)
  156. dev_dbg(ispi->dev, "FDATA(%d)=0x%08x\n",
  157. i, readl(ispi->base + FDATA(i)));
  158. dev_dbg(ispi->dev, "FRACC=0x%08x\n", readl(ispi->base + FRACC));
  159. for (i = 0; i < ispi->nregions; i++)
  160. dev_dbg(ispi->dev, "FREG(%d)=0x%08x\n", i,
  161. readl(ispi->base + FREG(i)));
  162. for (i = 0; i < ispi->pr_num; i++)
  163. dev_dbg(ispi->dev, "PR(%d)=0x%08x\n", i,
  164. readl(ispi->pregs + PR(i)));
  165. value = readl(ispi->sregs + SSFSTS_CTL);
  166. dev_dbg(ispi->dev, "SSFSTS_CTL=0x%08x\n", value);
  167. dev_dbg(ispi->dev, "PREOP_OPTYPE=0x%08x\n",
  168. readl(ispi->sregs + PREOP_OPTYPE));
  169. dev_dbg(ispi->dev, "OPMENU0=0x%08x\n", readl(ispi->sregs + OPMENU0));
  170. dev_dbg(ispi->dev, "OPMENU1=0x%08x\n", readl(ispi->sregs + OPMENU1));
  171. if (ispi->info->type == INTEL_SPI_BYT)
  172. dev_dbg(ispi->dev, "BCR=0x%08x\n", readl(ispi->base + BYT_BCR));
  173. dev_dbg(ispi->dev, "LVSCC=0x%08x\n", readl(ispi->base + LVSCC));
  174. dev_dbg(ispi->dev, "UVSCC=0x%08x\n", readl(ispi->base + UVSCC));
  175. dev_dbg(ispi->dev, "Protected regions:\n");
  176. for (i = 0; i < ispi->pr_num; i++) {
  177. u32 base, limit;
  178. value = readl(ispi->pregs + PR(i));
  179. if (!(value & (PR_WPE | PR_RPE)))
  180. continue;
  181. limit = (value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
  182. base = value & PR_BASE_MASK;
  183. dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x [%c%c]\n",
  184. i, base << 12, (limit << 12) | 0xfff,
  185. value & PR_WPE ? 'W' : '.',
  186. value & PR_RPE ? 'R' : '.');
  187. }
  188. dev_dbg(ispi->dev, "Flash regions:\n");
  189. for (i = 0; i < ispi->nregions; i++) {
  190. u32 region, base, limit;
  191. region = readl(ispi->base + FREG(i));
  192. base = region & FREG_BASE_MASK;
  193. limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
  194. if (base >= limit || (i > 0 && limit == 0))
  195. dev_dbg(ispi->dev, " %02d disabled\n", i);
  196. else
  197. dev_dbg(ispi->dev, " %02d base: 0x%08x limit: 0x%08x\n",
  198. i, base << 12, (limit << 12) | 0xfff);
  199. }
  200. dev_dbg(ispi->dev, "Using %cW sequencer for register access\n",
  201. ispi->swseq_reg ? 'S' : 'H');
  202. dev_dbg(ispi->dev, "Using %cW sequencer for erase operation\n",
  203. ispi->swseq_erase ? 'S' : 'H');
  204. }
  205. /* Reads max INTEL_SPI_FIFO_SZ bytes from the device fifo */
  206. static int intel_spi_read_block(struct intel_spi *ispi, void *buf, size_t size)
  207. {
  208. size_t bytes;
  209. int i = 0;
  210. if (size > INTEL_SPI_FIFO_SZ)
  211. return -EINVAL;
  212. while (size > 0) {
  213. bytes = min_t(size_t, size, 4);
  214. memcpy_fromio(buf, ispi->base + FDATA(i), bytes);
  215. size -= bytes;
  216. buf += bytes;
  217. i++;
  218. }
  219. return 0;
  220. }
  221. /* Writes max INTEL_SPI_FIFO_SZ bytes to the device fifo */
  222. static int intel_spi_write_block(struct intel_spi *ispi, const void *buf,
  223. size_t size)
  224. {
  225. size_t bytes;
  226. int i = 0;
  227. if (size > INTEL_SPI_FIFO_SZ)
  228. return -EINVAL;
  229. while (size > 0) {
  230. bytes = min_t(size_t, size, 4);
  231. memcpy_toio(ispi->base + FDATA(i), buf, bytes);
  232. size -= bytes;
  233. buf += bytes;
  234. i++;
  235. }
  236. return 0;
  237. }
  238. static int intel_spi_wait_hw_busy(struct intel_spi *ispi)
  239. {
  240. u32 val;
  241. return readl_poll_timeout(ispi->base + HSFSTS_CTL, val,
  242. !(val & HSFSTS_CTL_SCIP), 40,
  243. INTEL_SPI_TIMEOUT * 1000);
  244. }
  245. static int intel_spi_wait_sw_busy(struct intel_spi *ispi)
  246. {
  247. u32 val;
  248. return readl_poll_timeout(ispi->sregs + SSFSTS_CTL, val,
  249. !(val & SSFSTS_CTL_SCIP), 40,
  250. INTEL_SPI_TIMEOUT * 1000);
  251. }
  252. static int intel_spi_init(struct intel_spi *ispi)
  253. {
  254. u32 opmenu0, opmenu1, lvscc, uvscc, val;
  255. int i;
  256. switch (ispi->info->type) {
  257. case INTEL_SPI_BYT:
  258. ispi->sregs = ispi->base + BYT_SSFSTS_CTL;
  259. ispi->pregs = ispi->base + BYT_PR;
  260. ispi->nregions = BYT_FREG_NUM;
  261. ispi->pr_num = BYT_PR_NUM;
  262. ispi->swseq_reg = true;
  263. if (writeable) {
  264. /* Disable write protection */
  265. val = readl(ispi->base + BYT_BCR);
  266. if (!(val & BYT_BCR_WPD)) {
  267. val |= BYT_BCR_WPD;
  268. writel(val, ispi->base + BYT_BCR);
  269. val = readl(ispi->base + BYT_BCR);
  270. }
  271. ispi->writeable = !!(val & BYT_BCR_WPD);
  272. }
  273. break;
  274. case INTEL_SPI_LPT:
  275. ispi->sregs = ispi->base + LPT_SSFSTS_CTL;
  276. ispi->pregs = ispi->base + LPT_PR;
  277. ispi->nregions = LPT_FREG_NUM;
  278. ispi->pr_num = LPT_PR_NUM;
  279. ispi->swseq_reg = true;
  280. break;
  281. case INTEL_SPI_BXT:
  282. ispi->sregs = ispi->base + BXT_SSFSTS_CTL;
  283. ispi->pregs = ispi->base + BXT_PR;
  284. ispi->nregions = BXT_FREG_NUM;
  285. ispi->pr_num = BXT_PR_NUM;
  286. ispi->erase_64k = true;
  287. break;
  288. default:
  289. return -EINVAL;
  290. }
  291. /* Disable #SMI generation from HW sequencer */
  292. val = readl(ispi->base + HSFSTS_CTL);
  293. val &= ~HSFSTS_CTL_FSMIE;
  294. writel(val, ispi->base + HSFSTS_CTL);
  295. /*
  296. * Determine whether erase operation should use HW or SW sequencer.
  297. *
  298. * The HW sequencer has a predefined list of opcodes, with only the
  299. * erase opcode being programmable in LVSCC and UVSCC registers.
  300. * If these registers don't contain a valid erase opcode, erase
  301. * cannot be done using HW sequencer.
  302. */
  303. lvscc = readl(ispi->base + LVSCC);
  304. uvscc = readl(ispi->base + UVSCC);
  305. if (!(lvscc & ERASE_OPCODE_MASK) || !(uvscc & ERASE_OPCODE_MASK))
  306. ispi->swseq_erase = true;
  307. /* SPI controller on Intel BXT supports 64K erase opcode */
  308. if (ispi->info->type == INTEL_SPI_BXT && !ispi->swseq_erase)
  309. if (!(lvscc & ERASE_64K_OPCODE_MASK) ||
  310. !(uvscc & ERASE_64K_OPCODE_MASK))
  311. ispi->erase_64k = false;
  312. /*
  313. * Some controllers can only do basic operations using hardware
  314. * sequencer. All other operations are supposed to be carried out
  315. * using software sequencer.
  316. */
  317. if (ispi->swseq_reg) {
  318. /* Disable #SMI generation from SW sequencer */
  319. val = readl(ispi->sregs + SSFSTS_CTL);
  320. val &= ~SSFSTS_CTL_FSMIE;
  321. writel(val, ispi->sregs + SSFSTS_CTL);
  322. }
  323. /* Check controller's lock status */
  324. val = readl(ispi->base + HSFSTS_CTL);
  325. ispi->locked = !!(val & HSFSTS_CTL_FLOCKDN);
  326. if (ispi->locked) {
  327. /*
  328. * BIOS programs allowed opcodes and then locks down the
  329. * register. So read back what opcodes it decided to support.
  330. * That's the set we are going to support as well.
  331. */
  332. opmenu0 = readl(ispi->sregs + OPMENU0);
  333. opmenu1 = readl(ispi->sregs + OPMENU1);
  334. if (opmenu0 && opmenu1) {
  335. for (i = 0; i < ARRAY_SIZE(ispi->opcodes) / 2; i++) {
  336. ispi->opcodes[i] = opmenu0 >> i * 8;
  337. ispi->opcodes[i + 4] = opmenu1 >> i * 8;
  338. }
  339. }
  340. }
  341. intel_spi_dump_regs(ispi);
  342. return 0;
  343. }
  344. static int intel_spi_opcode_index(struct intel_spi *ispi, u8 opcode, int optype)
  345. {
  346. int i;
  347. int preop;
  348. if (ispi->locked) {
  349. for (i = 0; i < ARRAY_SIZE(ispi->opcodes); i++)
  350. if (ispi->opcodes[i] == opcode)
  351. return i;
  352. return -EINVAL;
  353. }
  354. /* The lock is off, so just use index 0 */
  355. writel(opcode, ispi->sregs + OPMENU0);
  356. preop = readw(ispi->sregs + PREOP_OPTYPE);
  357. writel(optype << 16 | preop, ispi->sregs + PREOP_OPTYPE);
  358. return 0;
  359. }
  360. static int intel_spi_hw_cycle(struct intel_spi *ispi, u8 opcode, int len)
  361. {
  362. u32 val, status;
  363. int ret;
  364. val = readl(ispi->base + HSFSTS_CTL);
  365. val &= ~(HSFSTS_CTL_FCYCLE_MASK | HSFSTS_CTL_FDBC_MASK);
  366. switch (opcode) {
  367. case SPINOR_OP_RDID:
  368. val |= HSFSTS_CTL_FCYCLE_RDID;
  369. break;
  370. case SPINOR_OP_WRSR:
  371. val |= HSFSTS_CTL_FCYCLE_WRSR;
  372. break;
  373. case SPINOR_OP_RDSR:
  374. val |= HSFSTS_CTL_FCYCLE_RDSR;
  375. break;
  376. default:
  377. return -EINVAL;
  378. }
  379. if (len > INTEL_SPI_FIFO_SZ)
  380. return -EINVAL;
  381. val |= (len - 1) << HSFSTS_CTL_FDBC_SHIFT;
  382. val |= HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  383. val |= HSFSTS_CTL_FGO;
  384. writel(val, ispi->base + HSFSTS_CTL);
  385. ret = intel_spi_wait_hw_busy(ispi);
  386. if (ret)
  387. return ret;
  388. status = readl(ispi->base + HSFSTS_CTL);
  389. if (status & HSFSTS_CTL_FCERR)
  390. return -EIO;
  391. else if (status & HSFSTS_CTL_AEL)
  392. return -EACCES;
  393. return 0;
  394. }
  395. static int intel_spi_sw_cycle(struct intel_spi *ispi, u8 opcode, int len,
  396. int optype)
  397. {
  398. u32 val = 0, status;
  399. u8 atomic_preopcode;
  400. int ret;
  401. ret = intel_spi_opcode_index(ispi, opcode, optype);
  402. if (ret < 0)
  403. return ret;
  404. if (len > INTEL_SPI_FIFO_SZ)
  405. return -EINVAL;
  406. /*
  407. * Always clear it after each SW sequencer operation regardless
  408. * of whether it is successful or not.
  409. */
  410. atomic_preopcode = ispi->atomic_preopcode;
  411. ispi->atomic_preopcode = 0;
  412. /* Only mark 'Data Cycle' bit when there is data to be transferred */
  413. if (len > 0)
  414. val = ((len - 1) << SSFSTS_CTL_DBC_SHIFT) | SSFSTS_CTL_DS;
  415. val |= ret << SSFSTS_CTL_COP_SHIFT;
  416. val |= SSFSTS_CTL_FCERR | SSFSTS_CTL_FDONE;
  417. val |= SSFSTS_CTL_SCGO;
  418. if (atomic_preopcode) {
  419. u16 preop;
  420. switch (optype) {
  421. case OPTYPE_WRITE_NO_ADDR:
  422. case OPTYPE_WRITE_WITH_ADDR:
  423. /* Pick matching preopcode for the atomic sequence */
  424. preop = readw(ispi->sregs + PREOP_OPTYPE);
  425. if ((preop & 0xff) == atomic_preopcode)
  426. ; /* Do nothing */
  427. else if ((preop >> 8) == atomic_preopcode)
  428. val |= SSFSTS_CTL_SPOP;
  429. else
  430. return -EINVAL;
  431. /* Enable atomic sequence */
  432. val |= SSFSTS_CTL_ACS;
  433. break;
  434. default:
  435. return -EINVAL;
  436. }
  437. }
  438. writel(val, ispi->sregs + SSFSTS_CTL);
  439. ret = intel_spi_wait_sw_busy(ispi);
  440. if (ret)
  441. return ret;
  442. status = readl(ispi->sregs + SSFSTS_CTL);
  443. if (status & SSFSTS_CTL_FCERR)
  444. return -EIO;
  445. else if (status & SSFSTS_CTL_AEL)
  446. return -EACCES;
  447. return 0;
  448. }
  449. static int intel_spi_read_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  450. {
  451. struct intel_spi *ispi = nor->priv;
  452. int ret;
  453. /* Address of the first chip */
  454. writel(0, ispi->base + FADDR);
  455. if (ispi->swseq_reg)
  456. ret = intel_spi_sw_cycle(ispi, opcode, len,
  457. OPTYPE_READ_NO_ADDR);
  458. else
  459. ret = intel_spi_hw_cycle(ispi, opcode, len);
  460. if (ret)
  461. return ret;
  462. return intel_spi_read_block(ispi, buf, len);
  463. }
  464. static int intel_spi_write_reg(struct spi_nor *nor, u8 opcode, u8 *buf, int len)
  465. {
  466. struct intel_spi *ispi = nor->priv;
  467. int ret;
  468. /*
  469. * This is handled with atomic operation and preop code in Intel
  470. * controller so we only verify that it is available. If the
  471. * controller is not locked, program the opcode to the PREOP
  472. * register for later use.
  473. *
  474. * When hardware sequencer is used there is no need to program
  475. * any opcodes (it handles them automatically as part of a command).
  476. */
  477. if (opcode == SPINOR_OP_WREN) {
  478. u16 preop;
  479. if (!ispi->swseq_reg)
  480. return 0;
  481. preop = readw(ispi->sregs + PREOP_OPTYPE);
  482. if ((preop & 0xff) != opcode && (preop >> 8) != opcode) {
  483. if (ispi->locked)
  484. return -EINVAL;
  485. writel(opcode, ispi->sregs + PREOP_OPTYPE);
  486. }
  487. /*
  488. * This enables atomic sequence on next SW sycle. Will
  489. * be cleared after next operation.
  490. */
  491. ispi->atomic_preopcode = opcode;
  492. return 0;
  493. }
  494. writel(0, ispi->base + FADDR);
  495. /* Write the value beforehand */
  496. ret = intel_spi_write_block(ispi, buf, len);
  497. if (ret)
  498. return ret;
  499. if (ispi->swseq_reg)
  500. return intel_spi_sw_cycle(ispi, opcode, len,
  501. OPTYPE_WRITE_NO_ADDR);
  502. return intel_spi_hw_cycle(ispi, opcode, len);
  503. }
  504. static ssize_t intel_spi_read(struct spi_nor *nor, loff_t from, size_t len,
  505. u_char *read_buf)
  506. {
  507. struct intel_spi *ispi = nor->priv;
  508. size_t block_size, retlen = 0;
  509. u32 val, status;
  510. ssize_t ret;
  511. /*
  512. * Atomic sequence is not expected with HW sequencer reads. Make
  513. * sure it is cleared regardless.
  514. */
  515. if (WARN_ON_ONCE(ispi->atomic_preopcode))
  516. ispi->atomic_preopcode = 0;
  517. switch (nor->read_opcode) {
  518. case SPINOR_OP_READ:
  519. case SPINOR_OP_READ_FAST:
  520. break;
  521. default:
  522. return -EINVAL;
  523. }
  524. while (len > 0) {
  525. block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
  526. /* Read cannot cross 4K boundary */
  527. block_size = min_t(loff_t, from + block_size,
  528. round_up(from + 1, SZ_4K)) - from;
  529. writel(from, ispi->base + FADDR);
  530. val = readl(ispi->base + HSFSTS_CTL);
  531. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  532. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  533. val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
  534. val |= HSFSTS_CTL_FCYCLE_READ;
  535. val |= HSFSTS_CTL_FGO;
  536. writel(val, ispi->base + HSFSTS_CTL);
  537. ret = intel_spi_wait_hw_busy(ispi);
  538. if (ret)
  539. return ret;
  540. status = readl(ispi->base + HSFSTS_CTL);
  541. if (status & HSFSTS_CTL_FCERR)
  542. ret = -EIO;
  543. else if (status & HSFSTS_CTL_AEL)
  544. ret = -EACCES;
  545. if (ret < 0) {
  546. dev_err(ispi->dev, "read error: %llx: %#x\n", from,
  547. status);
  548. return ret;
  549. }
  550. ret = intel_spi_read_block(ispi, read_buf, block_size);
  551. if (ret)
  552. return ret;
  553. len -= block_size;
  554. from += block_size;
  555. retlen += block_size;
  556. read_buf += block_size;
  557. }
  558. return retlen;
  559. }
  560. static ssize_t intel_spi_write(struct spi_nor *nor, loff_t to, size_t len,
  561. const u_char *write_buf)
  562. {
  563. struct intel_spi *ispi = nor->priv;
  564. size_t block_size, retlen = 0;
  565. u32 val, status;
  566. ssize_t ret;
  567. /* Not needed with HW sequencer write, make sure it is cleared */
  568. ispi->atomic_preopcode = 0;
  569. while (len > 0) {
  570. block_size = min_t(size_t, len, INTEL_SPI_FIFO_SZ);
  571. /* Write cannot cross 4K boundary */
  572. block_size = min_t(loff_t, to + block_size,
  573. round_up(to + 1, SZ_4K)) - to;
  574. writel(to, ispi->base + FADDR);
  575. val = readl(ispi->base + HSFSTS_CTL);
  576. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  577. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  578. val |= (block_size - 1) << HSFSTS_CTL_FDBC_SHIFT;
  579. val |= HSFSTS_CTL_FCYCLE_WRITE;
  580. ret = intel_spi_write_block(ispi, write_buf, block_size);
  581. if (ret) {
  582. dev_err(ispi->dev, "failed to write block\n");
  583. return ret;
  584. }
  585. /* Start the write now */
  586. val |= HSFSTS_CTL_FGO;
  587. writel(val, ispi->base + HSFSTS_CTL);
  588. ret = intel_spi_wait_hw_busy(ispi);
  589. if (ret) {
  590. dev_err(ispi->dev, "timeout\n");
  591. return ret;
  592. }
  593. status = readl(ispi->base + HSFSTS_CTL);
  594. if (status & HSFSTS_CTL_FCERR)
  595. ret = -EIO;
  596. else if (status & HSFSTS_CTL_AEL)
  597. ret = -EACCES;
  598. if (ret < 0) {
  599. dev_err(ispi->dev, "write error: %llx: %#x\n", to,
  600. status);
  601. return ret;
  602. }
  603. len -= block_size;
  604. to += block_size;
  605. retlen += block_size;
  606. write_buf += block_size;
  607. }
  608. return retlen;
  609. }
  610. static int intel_spi_erase(struct spi_nor *nor, loff_t offs)
  611. {
  612. size_t erase_size, len = nor->mtd.erasesize;
  613. struct intel_spi *ispi = nor->priv;
  614. u32 val, status, cmd;
  615. int ret;
  616. /* If the hardware can do 64k erase use that when possible */
  617. if (len >= SZ_64K && ispi->erase_64k) {
  618. cmd = HSFSTS_CTL_FCYCLE_ERASE_64K;
  619. erase_size = SZ_64K;
  620. } else {
  621. cmd = HSFSTS_CTL_FCYCLE_ERASE;
  622. erase_size = SZ_4K;
  623. }
  624. if (ispi->swseq_erase) {
  625. while (len > 0) {
  626. writel(offs, ispi->base + FADDR);
  627. ret = intel_spi_sw_cycle(ispi, nor->erase_opcode,
  628. 0, OPTYPE_WRITE_WITH_ADDR);
  629. if (ret)
  630. return ret;
  631. offs += erase_size;
  632. len -= erase_size;
  633. }
  634. return 0;
  635. }
  636. /* Not needed with HW sequencer erase, make sure it is cleared */
  637. ispi->atomic_preopcode = 0;
  638. while (len > 0) {
  639. writel(offs, ispi->base + FADDR);
  640. val = readl(ispi->base + HSFSTS_CTL);
  641. val &= ~(HSFSTS_CTL_FDBC_MASK | HSFSTS_CTL_FCYCLE_MASK);
  642. val |= HSFSTS_CTL_AEL | HSFSTS_CTL_FCERR | HSFSTS_CTL_FDONE;
  643. val |= cmd;
  644. val |= HSFSTS_CTL_FGO;
  645. writel(val, ispi->base + HSFSTS_CTL);
  646. ret = intel_spi_wait_hw_busy(ispi);
  647. if (ret)
  648. return ret;
  649. status = readl(ispi->base + HSFSTS_CTL);
  650. if (status & HSFSTS_CTL_FCERR)
  651. return -EIO;
  652. else if (status & HSFSTS_CTL_AEL)
  653. return -EACCES;
  654. offs += erase_size;
  655. len -= erase_size;
  656. }
  657. return 0;
  658. }
  659. static bool intel_spi_is_protected(const struct intel_spi *ispi,
  660. unsigned int base, unsigned int limit)
  661. {
  662. int i;
  663. for (i = 0; i < ispi->pr_num; i++) {
  664. u32 pr_base, pr_limit, pr_value;
  665. pr_value = readl(ispi->pregs + PR(i));
  666. if (!(pr_value & (PR_WPE | PR_RPE)))
  667. continue;
  668. pr_limit = (pr_value & PR_LIMIT_MASK) >> PR_LIMIT_SHIFT;
  669. pr_base = pr_value & PR_BASE_MASK;
  670. if (pr_base >= base && pr_limit <= limit)
  671. return true;
  672. }
  673. return false;
  674. }
  675. /*
  676. * There will be a single partition holding all enabled flash regions. We
  677. * call this "BIOS".
  678. */
  679. static void intel_spi_fill_partition(struct intel_spi *ispi,
  680. struct mtd_partition *part)
  681. {
  682. u64 end;
  683. int i;
  684. memset(part, 0, sizeof(*part));
  685. /* Start from the mandatory descriptor region */
  686. part->size = 4096;
  687. part->name = "BIOS";
  688. /*
  689. * Now try to find where this partition ends based on the flash
  690. * region registers.
  691. */
  692. for (i = 1; i < ispi->nregions; i++) {
  693. u32 region, base, limit;
  694. region = readl(ispi->base + FREG(i));
  695. base = region & FREG_BASE_MASK;
  696. limit = (region & FREG_LIMIT_MASK) >> FREG_LIMIT_SHIFT;
  697. if (base >= limit || limit == 0)
  698. continue;
  699. /*
  700. * If any of the regions have protection bits set, make the
  701. * whole partition read-only to be on the safe side.
  702. */
  703. if (intel_spi_is_protected(ispi, base, limit))
  704. ispi->writeable = false;
  705. end = (limit << 12) + 4096;
  706. if (end > part->size)
  707. part->size = end;
  708. }
  709. }
  710. struct intel_spi *intel_spi_probe(struct device *dev,
  711. struct resource *mem, const struct intel_spi_boardinfo *info)
  712. {
  713. const struct spi_nor_hwcaps hwcaps = {
  714. .mask = SNOR_HWCAPS_READ |
  715. SNOR_HWCAPS_READ_FAST |
  716. SNOR_HWCAPS_PP,
  717. };
  718. struct mtd_partition part;
  719. struct intel_spi *ispi;
  720. int ret;
  721. if (!info || !mem)
  722. return ERR_PTR(-EINVAL);
  723. ispi = devm_kzalloc(dev, sizeof(*ispi), GFP_KERNEL);
  724. if (!ispi)
  725. return ERR_PTR(-ENOMEM);
  726. ispi->base = devm_ioremap_resource(dev, mem);
  727. if (IS_ERR(ispi->base))
  728. return ERR_CAST(ispi->base);
  729. ispi->dev = dev;
  730. ispi->info = info;
  731. ispi->writeable = info->writeable;
  732. ret = intel_spi_init(ispi);
  733. if (ret)
  734. return ERR_PTR(ret);
  735. ispi->nor.dev = ispi->dev;
  736. ispi->nor.priv = ispi;
  737. ispi->nor.read_reg = intel_spi_read_reg;
  738. ispi->nor.write_reg = intel_spi_write_reg;
  739. ispi->nor.read = intel_spi_read;
  740. ispi->nor.write = intel_spi_write;
  741. ispi->nor.erase = intel_spi_erase;
  742. ret = spi_nor_scan(&ispi->nor, NULL, &hwcaps);
  743. if (ret) {
  744. dev_info(dev, "failed to locate the chip\n");
  745. return ERR_PTR(ret);
  746. }
  747. intel_spi_fill_partition(ispi, &part);
  748. /* Prevent writes if not explicitly enabled */
  749. if (!ispi->writeable || !writeable)
  750. ispi->nor.mtd.flags &= ~MTD_WRITEABLE;
  751. ret = mtd_device_register(&ispi->nor.mtd, &part, 1);
  752. if (ret)
  753. return ERR_PTR(ret);
  754. return ispi;
  755. }
  756. EXPORT_SYMBOL_GPL(intel_spi_probe);
  757. int intel_spi_remove(struct intel_spi *ispi)
  758. {
  759. return mtd_device_unregister(&ispi->nor.mtd);
  760. }
  761. EXPORT_SYMBOL_GPL(intel_spi_remove);
  762. MODULE_DESCRIPTION("Intel PCH/PCU SPI flash core driver");
  763. MODULE_AUTHOR("Mika Westerberg <mika.westerberg@linux.intel.com>");
  764. MODULE_LICENSE("GPL v2");