intel-spi.c 21 KB

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