cafe_nand.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. /*
  2. * Driver for One Laptop Per Child ‘CAFÉ’ controller, aka Marvell 88ALP01
  3. *
  4. * The data sheet for this device can be found at:
  5. * http://wiki.laptop.org/go/Datasheets
  6. *
  7. * Copyright © 2006 Red Hat, Inc.
  8. * Copyright © 2006 David Woodhouse <dwmw2@infradead.org>
  9. */
  10. #define DEBUG
  11. #include <linux/device.h>
  12. #undef DEBUG
  13. #include <linux/mtd/mtd.h>
  14. #include <linux/mtd/nand.h>
  15. #include <linux/mtd/partitions.h>
  16. #include <linux/rslib.h>
  17. #include <linux/pci.h>
  18. #include <linux/delay.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/dma-mapping.h>
  21. #include <linux/slab.h>
  22. #include <asm/io.h>
  23. #define CAFE_NAND_CTRL1 0x00
  24. #define CAFE_NAND_CTRL2 0x04
  25. #define CAFE_NAND_CTRL3 0x08
  26. #define CAFE_NAND_STATUS 0x0c
  27. #define CAFE_NAND_IRQ 0x10
  28. #define CAFE_NAND_IRQ_MASK 0x14
  29. #define CAFE_NAND_DATA_LEN 0x18
  30. #define CAFE_NAND_ADDR1 0x1c
  31. #define CAFE_NAND_ADDR2 0x20
  32. #define CAFE_NAND_TIMING1 0x24
  33. #define CAFE_NAND_TIMING2 0x28
  34. #define CAFE_NAND_TIMING3 0x2c
  35. #define CAFE_NAND_NONMEM 0x30
  36. #define CAFE_NAND_ECC_RESULT 0x3C
  37. #define CAFE_NAND_DMA_CTRL 0x40
  38. #define CAFE_NAND_DMA_ADDR0 0x44
  39. #define CAFE_NAND_DMA_ADDR1 0x48
  40. #define CAFE_NAND_ECC_SYN01 0x50
  41. #define CAFE_NAND_ECC_SYN23 0x54
  42. #define CAFE_NAND_ECC_SYN45 0x58
  43. #define CAFE_NAND_ECC_SYN67 0x5c
  44. #define CAFE_NAND_READ_DATA 0x1000
  45. #define CAFE_NAND_WRITE_DATA 0x2000
  46. #define CAFE_GLOBAL_CTRL 0x3004
  47. #define CAFE_GLOBAL_IRQ 0x3008
  48. #define CAFE_GLOBAL_IRQ_MASK 0x300c
  49. #define CAFE_NAND_RESET 0x3034
  50. /* Missing from the datasheet: bit 19 of CTRL1 sets CE0 vs. CE1 */
  51. #define CTRL1_CHIPSELECT (1<<19)
  52. struct cafe_priv {
  53. struct nand_chip nand;
  54. struct mtd_partition *parts;
  55. struct pci_dev *pdev;
  56. void __iomem *mmio;
  57. struct rs_control *rs;
  58. uint32_t ctl1;
  59. uint32_t ctl2;
  60. int datalen;
  61. int nr_data;
  62. int data_pos;
  63. int page_addr;
  64. dma_addr_t dmaaddr;
  65. unsigned char *dmabuf;
  66. };
  67. static int usedma = 1;
  68. module_param(usedma, int, 0644);
  69. static int skipbbt = 0;
  70. module_param(skipbbt, int, 0644);
  71. static int debug = 0;
  72. module_param(debug, int, 0644);
  73. static int regdebug = 0;
  74. module_param(regdebug, int, 0644);
  75. static int checkecc = 1;
  76. module_param(checkecc, int, 0644);
  77. static unsigned int numtimings;
  78. static int timing[3];
  79. module_param_array(timing, int, &numtimings, 0644);
  80. #ifdef CONFIG_MTD_PARTITIONS
  81. static const char *part_probes[] = { "cmdlinepart", "RedBoot", NULL };
  82. #endif
  83. /* Hrm. Why isn't this already conditional on something in the struct device? */
  84. #define cafe_dev_dbg(dev, args...) do { if (debug) dev_dbg(dev, ##args); } while(0)
  85. /* Make it easier to switch to PIO if we need to */
  86. #define cafe_readl(cafe, addr) readl((cafe)->mmio + CAFE_##addr)
  87. #define cafe_writel(cafe, datum, addr) writel(datum, (cafe)->mmio + CAFE_##addr)
  88. static int cafe_device_ready(struct mtd_info *mtd)
  89. {
  90. struct cafe_priv *cafe = mtd->priv;
  91. int result = !!(cafe_readl(cafe, NAND_STATUS) | 0x40000000);
  92. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  93. cafe_writel(cafe, irqs, NAND_IRQ);
  94. cafe_dev_dbg(&cafe->pdev->dev, "NAND device is%s ready, IRQ %x (%x) (%x,%x)\n",
  95. result?"":" not", irqs, cafe_readl(cafe, NAND_IRQ),
  96. cafe_readl(cafe, GLOBAL_IRQ), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  97. return result;
  98. }
  99. static void cafe_write_buf(struct mtd_info *mtd, const uint8_t *buf, int len)
  100. {
  101. struct cafe_priv *cafe = mtd->priv;
  102. if (usedma)
  103. memcpy(cafe->dmabuf + cafe->datalen, buf, len);
  104. else
  105. memcpy_toio(cafe->mmio + CAFE_NAND_WRITE_DATA + cafe->datalen, buf, len);
  106. cafe->datalen += len;
  107. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes to write buffer. datalen 0x%x\n",
  108. len, cafe->datalen);
  109. }
  110. static void cafe_read_buf(struct mtd_info *mtd, uint8_t *buf, int len)
  111. {
  112. struct cafe_priv *cafe = mtd->priv;
  113. if (usedma)
  114. memcpy(buf, cafe->dmabuf + cafe->datalen, len);
  115. else
  116. memcpy_fromio(buf, cafe->mmio + CAFE_NAND_READ_DATA + cafe->datalen, len);
  117. cafe_dev_dbg(&cafe->pdev->dev, "Copy 0x%x bytes from position 0x%x in read buffer.\n",
  118. len, cafe->datalen);
  119. cafe->datalen += len;
  120. }
  121. static uint8_t cafe_read_byte(struct mtd_info *mtd)
  122. {
  123. struct cafe_priv *cafe = mtd->priv;
  124. uint8_t d;
  125. cafe_read_buf(mtd, &d, 1);
  126. cafe_dev_dbg(&cafe->pdev->dev, "Read %02x\n", d);
  127. return d;
  128. }
  129. static void cafe_nand_cmdfunc(struct mtd_info *mtd, unsigned command,
  130. int column, int page_addr)
  131. {
  132. struct cafe_priv *cafe = mtd->priv;
  133. int adrbytes = 0;
  134. uint32_t ctl1;
  135. uint32_t doneint = 0x80000000;
  136. cafe_dev_dbg(&cafe->pdev->dev, "cmdfunc %02x, 0x%x, 0x%x\n",
  137. command, column, page_addr);
  138. if (command == NAND_CMD_ERASE2 || command == NAND_CMD_PAGEPROG) {
  139. /* Second half of a command we already calculated */
  140. cafe_writel(cafe, cafe->ctl2 | 0x100 | command, NAND_CTRL2);
  141. ctl1 = cafe->ctl1;
  142. cafe->ctl2 &= ~(1<<30);
  143. cafe_dev_dbg(&cafe->pdev->dev, "Continue command, ctl1 %08x, #data %d\n",
  144. cafe->ctl1, cafe->nr_data);
  145. goto do_command;
  146. }
  147. /* Reset ECC engine */
  148. cafe_writel(cafe, 0, NAND_CTRL2);
  149. /* Emulate NAND_CMD_READOOB on large-page chips */
  150. if (mtd->writesize > 512 &&
  151. command == NAND_CMD_READOOB) {
  152. column += mtd->writesize;
  153. command = NAND_CMD_READ0;
  154. }
  155. /* FIXME: Do we need to send read command before sending data
  156. for small-page chips, to position the buffer correctly? */
  157. if (column != -1) {
  158. cafe_writel(cafe, column, NAND_ADDR1);
  159. adrbytes = 2;
  160. if (page_addr != -1)
  161. goto write_adr2;
  162. } else if (page_addr != -1) {
  163. cafe_writel(cafe, page_addr & 0xffff, NAND_ADDR1);
  164. page_addr >>= 16;
  165. write_adr2:
  166. cafe_writel(cafe, page_addr, NAND_ADDR2);
  167. adrbytes += 2;
  168. if (mtd->size > mtd->writesize << 16)
  169. adrbytes++;
  170. }
  171. cafe->data_pos = cafe->datalen = 0;
  172. /* Set command valid bit, mask in the chip select bit */
  173. ctl1 = 0x80000000 | command | (cafe->ctl1 & CTRL1_CHIPSELECT);
  174. /* Set RD or WR bits as appropriate */
  175. if (command == NAND_CMD_READID || command == NAND_CMD_STATUS) {
  176. ctl1 |= (1<<26); /* rd */
  177. /* Always 5 bytes, for now */
  178. cafe->datalen = 4;
  179. /* And one address cycle -- even for STATUS, since the controller doesn't work without */
  180. adrbytes = 1;
  181. } else if (command == NAND_CMD_READ0 || command == NAND_CMD_READ1 ||
  182. command == NAND_CMD_READOOB || command == NAND_CMD_RNDOUT) {
  183. ctl1 |= 1<<26; /* rd */
  184. /* For now, assume just read to end of page */
  185. cafe->datalen = mtd->writesize + mtd->oobsize - column;
  186. } else if (command == NAND_CMD_SEQIN)
  187. ctl1 |= 1<<25; /* wr */
  188. /* Set number of address bytes */
  189. if (adrbytes)
  190. ctl1 |= ((adrbytes-1)|8) << 27;
  191. if (command == NAND_CMD_SEQIN || command == NAND_CMD_ERASE1) {
  192. /* Ignore the first command of a pair; the hardware
  193. deals with them both at once, later */
  194. cafe->ctl1 = ctl1;
  195. cafe_dev_dbg(&cafe->pdev->dev, "Setup for delayed command, ctl1 %08x, dlen %x\n",
  196. cafe->ctl1, cafe->datalen);
  197. return;
  198. }
  199. /* RNDOUT and READ0 commands need a following byte */
  200. if (command == NAND_CMD_RNDOUT)
  201. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_RNDOUTSTART, NAND_CTRL2);
  202. else if (command == NAND_CMD_READ0 && mtd->writesize > 512)
  203. cafe_writel(cafe, cafe->ctl2 | 0x100 | NAND_CMD_READSTART, NAND_CTRL2);
  204. do_command:
  205. cafe_dev_dbg(&cafe->pdev->dev, "dlen %x, ctl1 %x, ctl2 %x\n",
  206. cafe->datalen, ctl1, cafe_readl(cafe, NAND_CTRL2));
  207. /* NB: The datasheet lies -- we really should be subtracting 1 here */
  208. cafe_writel(cafe, cafe->datalen, NAND_DATA_LEN);
  209. cafe_writel(cafe, 0x90000000, NAND_IRQ);
  210. if (usedma && (ctl1 & (3<<25))) {
  211. uint32_t dmactl = 0xc0000000 + cafe->datalen;
  212. /* If WR or RD bits set, set up DMA */
  213. if (ctl1 & (1<<26)) {
  214. /* It's a read */
  215. dmactl |= (1<<29);
  216. /* ... so it's done when the DMA is done, not just
  217. the command. */
  218. doneint = 0x10000000;
  219. }
  220. cafe_writel(cafe, dmactl, NAND_DMA_CTRL);
  221. }
  222. cafe->datalen = 0;
  223. if (unlikely(regdebug)) {
  224. int i;
  225. printk("About to write command %08x to register 0\n", ctl1);
  226. for (i=4; i< 0x5c; i+=4)
  227. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  228. }
  229. cafe_writel(cafe, ctl1, NAND_CTRL1);
  230. /* Apply this short delay always to ensure that we do wait tWB in
  231. * any case on any machine. */
  232. ndelay(100);
  233. if (1) {
  234. int c;
  235. uint32_t irqs;
  236. for (c = 500000; c != 0; c--) {
  237. irqs = cafe_readl(cafe, NAND_IRQ);
  238. if (irqs & doneint)
  239. break;
  240. udelay(1);
  241. if (!(c % 100000))
  242. cafe_dev_dbg(&cafe->pdev->dev, "Wait for ready, IRQ %x\n", irqs);
  243. cpu_relax();
  244. }
  245. cafe_writel(cafe, doneint, NAND_IRQ);
  246. cafe_dev_dbg(&cafe->pdev->dev, "Command %x completed after %d usec, irqs %x (%x)\n",
  247. command, 500000-c, irqs, cafe_readl(cafe, NAND_IRQ));
  248. }
  249. WARN_ON(cafe->ctl2 & (1<<30));
  250. switch (command) {
  251. case NAND_CMD_CACHEDPROG:
  252. case NAND_CMD_PAGEPROG:
  253. case NAND_CMD_ERASE1:
  254. case NAND_CMD_ERASE2:
  255. case NAND_CMD_SEQIN:
  256. case NAND_CMD_RNDIN:
  257. case NAND_CMD_STATUS:
  258. case NAND_CMD_DEPLETE1:
  259. case NAND_CMD_RNDOUT:
  260. case NAND_CMD_STATUS_ERROR:
  261. case NAND_CMD_STATUS_ERROR0:
  262. case NAND_CMD_STATUS_ERROR1:
  263. case NAND_CMD_STATUS_ERROR2:
  264. case NAND_CMD_STATUS_ERROR3:
  265. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  266. return;
  267. }
  268. nand_wait_ready(mtd);
  269. cafe_writel(cafe, cafe->ctl2, NAND_CTRL2);
  270. }
  271. static void cafe_select_chip(struct mtd_info *mtd, int chipnr)
  272. {
  273. struct cafe_priv *cafe = mtd->priv;
  274. cafe_dev_dbg(&cafe->pdev->dev, "select_chip %d\n", chipnr);
  275. /* Mask the appropriate bit into the stored value of ctl1
  276. which will be used by cafe_nand_cmdfunc() */
  277. if (chipnr)
  278. cafe->ctl1 |= CTRL1_CHIPSELECT;
  279. else
  280. cafe->ctl1 &= ~CTRL1_CHIPSELECT;
  281. }
  282. static irqreturn_t cafe_nand_interrupt(int irq, void *id)
  283. {
  284. struct mtd_info *mtd = id;
  285. struct cafe_priv *cafe = mtd->priv;
  286. uint32_t irqs = cafe_readl(cafe, NAND_IRQ);
  287. cafe_writel(cafe, irqs & ~0x90000000, NAND_IRQ);
  288. if (!irqs)
  289. return IRQ_NONE;
  290. cafe_dev_dbg(&cafe->pdev->dev, "irq, bits %x (%x)\n", irqs, cafe_readl(cafe, NAND_IRQ));
  291. return IRQ_HANDLED;
  292. }
  293. static void cafe_nand_bug(struct mtd_info *mtd)
  294. {
  295. BUG();
  296. }
  297. static int cafe_nand_write_oob(struct mtd_info *mtd,
  298. struct nand_chip *chip, int page)
  299. {
  300. int status = 0;
  301. chip->cmdfunc(mtd, NAND_CMD_SEQIN, mtd->writesize, page);
  302. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  303. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  304. status = chip->waitfunc(mtd, chip);
  305. return status & NAND_STATUS_FAIL ? -EIO : 0;
  306. }
  307. /* Don't use -- use nand_read_oob_std for now */
  308. static int cafe_nand_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
  309. int page, int sndcmd)
  310. {
  311. chip->cmdfunc(mtd, NAND_CMD_READOOB, 0, page);
  312. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  313. return 1;
  314. }
  315. /**
  316. * cafe_nand_read_page_syndrome - {REPLACABLE] hardware ecc syndrom based page read
  317. * @mtd: mtd info structure
  318. * @chip: nand chip info structure
  319. * @buf: buffer to store read data
  320. *
  321. * The hw generator calculates the error syndrome automatically. Therefor
  322. * we need a special oob layout and handling.
  323. */
  324. static int cafe_nand_read_page(struct mtd_info *mtd, struct nand_chip *chip,
  325. uint8_t *buf, int page)
  326. {
  327. struct cafe_priv *cafe = mtd->priv;
  328. cafe_dev_dbg(&cafe->pdev->dev, "ECC result %08x SYN1,2 %08x\n",
  329. cafe_readl(cafe, NAND_ECC_RESULT),
  330. cafe_readl(cafe, NAND_ECC_SYN01));
  331. chip->read_buf(mtd, buf, mtd->writesize);
  332. chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
  333. if (checkecc && cafe_readl(cafe, NAND_ECC_RESULT) & (1<<18)) {
  334. unsigned short syn[8], pat[4];
  335. int pos[4];
  336. u8 *oob = chip->oob_poi;
  337. int i, n;
  338. for (i=0; i<8; i+=2) {
  339. uint32_t tmp = cafe_readl(cafe, NAND_ECC_SYN01 + (i*2));
  340. syn[i] = cafe->rs->index_of[tmp & 0xfff];
  341. syn[i+1] = cafe->rs->index_of[(tmp >> 16) & 0xfff];
  342. }
  343. n = decode_rs16(cafe->rs, NULL, NULL, 1367, syn, 0, pos, 0,
  344. pat);
  345. for (i = 0; i < n; i++) {
  346. int p = pos[i];
  347. /* The 12-bit symbols are mapped to bytes here */
  348. if (p > 1374) {
  349. /* out of range */
  350. n = -1374;
  351. } else if (p == 0) {
  352. /* high four bits do not correspond to data */
  353. if (pat[i] > 0xff)
  354. n = -2048;
  355. else
  356. buf[0] ^= pat[i];
  357. } else if (p == 1365) {
  358. buf[2047] ^= pat[i] >> 4;
  359. oob[0] ^= pat[i] << 4;
  360. } else if (p > 1365) {
  361. if ((p & 1) == 1) {
  362. oob[3*p/2 - 2048] ^= pat[i] >> 4;
  363. oob[3*p/2 - 2047] ^= pat[i] << 4;
  364. } else {
  365. oob[3*p/2 - 2049] ^= pat[i] >> 8;
  366. oob[3*p/2 - 2048] ^= pat[i];
  367. }
  368. } else if ((p & 1) == 1) {
  369. buf[3*p/2] ^= pat[i] >> 4;
  370. buf[3*p/2 + 1] ^= pat[i] << 4;
  371. } else {
  372. buf[3*p/2 - 1] ^= pat[i] >> 8;
  373. buf[3*p/2] ^= pat[i];
  374. }
  375. }
  376. if (n < 0) {
  377. dev_dbg(&cafe->pdev->dev, "Failed to correct ECC at %08x\n",
  378. cafe_readl(cafe, NAND_ADDR2) * 2048);
  379. for (i = 0; i < 0x5c; i += 4)
  380. printk("Register %x: %08x\n", i, readl(cafe->mmio + i));
  381. mtd->ecc_stats.failed++;
  382. } else {
  383. dev_dbg(&cafe->pdev->dev, "Corrected %d symbol errors\n", n);
  384. mtd->ecc_stats.corrected += n;
  385. }
  386. }
  387. return 0;
  388. }
  389. static struct nand_ecclayout cafe_oobinfo_2048 = {
  390. .eccbytes = 14,
  391. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  392. .oobfree = {{14, 50}}
  393. };
  394. /* Ick. The BBT code really ought to be able to work this bit out
  395. for itself from the above, at least for the 2KiB case */
  396. static uint8_t cafe_bbt_pattern_2048[] = { 'B', 'b', 't', '0' };
  397. static uint8_t cafe_mirror_pattern_2048[] = { '1', 't', 'b', 'B' };
  398. static uint8_t cafe_bbt_pattern_512[] = { 0xBB };
  399. static uint8_t cafe_mirror_pattern_512[] = { 0xBC };
  400. static struct nand_bbt_descr cafe_bbt_main_descr_2048 = {
  401. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  402. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  403. .offs = 14,
  404. .len = 4,
  405. .veroffs = 18,
  406. .maxblocks = 4,
  407. .pattern = cafe_bbt_pattern_2048
  408. };
  409. static struct nand_bbt_descr cafe_bbt_mirror_descr_2048 = {
  410. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  411. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  412. .offs = 14,
  413. .len = 4,
  414. .veroffs = 18,
  415. .maxblocks = 4,
  416. .pattern = cafe_mirror_pattern_2048
  417. };
  418. static struct nand_ecclayout cafe_oobinfo_512 = {
  419. .eccbytes = 14,
  420. .eccpos = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13},
  421. .oobfree = {{14, 2}}
  422. };
  423. static struct nand_bbt_descr cafe_bbt_main_descr_512 = {
  424. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  425. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  426. .offs = 14,
  427. .len = 1,
  428. .veroffs = 15,
  429. .maxblocks = 4,
  430. .pattern = cafe_bbt_pattern_512
  431. };
  432. static struct nand_bbt_descr cafe_bbt_mirror_descr_512 = {
  433. .options = NAND_BBT_LASTBLOCK | NAND_BBT_CREATE | NAND_BBT_WRITE
  434. | NAND_BBT_2BIT | NAND_BBT_VERSION,
  435. .offs = 14,
  436. .len = 1,
  437. .veroffs = 15,
  438. .maxblocks = 4,
  439. .pattern = cafe_mirror_pattern_512
  440. };
  441. static void cafe_nand_write_page_lowlevel(struct mtd_info *mtd,
  442. struct nand_chip *chip, const uint8_t *buf)
  443. {
  444. struct cafe_priv *cafe = mtd->priv;
  445. chip->write_buf(mtd, buf, mtd->writesize);
  446. chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
  447. /* Set up ECC autogeneration */
  448. cafe->ctl2 |= (1<<30);
  449. }
  450. static int cafe_nand_write_page(struct mtd_info *mtd, struct nand_chip *chip,
  451. const uint8_t *buf, int page, int cached, int raw)
  452. {
  453. int status;
  454. chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
  455. if (unlikely(raw))
  456. chip->ecc.write_page_raw(mtd, chip, buf);
  457. else
  458. chip->ecc.write_page(mtd, chip, buf);
  459. /*
  460. * Cached progamming disabled for now, Not sure if its worth the
  461. * trouble. The speed gain is not very impressive. (2.3->2.6Mib/s)
  462. */
  463. cached = 0;
  464. if (!cached || !(chip->options & NAND_CACHEPRG)) {
  465. chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
  466. status = chip->waitfunc(mtd, chip);
  467. /*
  468. * See if operation failed and additional status checks are
  469. * available
  470. */
  471. if ((status & NAND_STATUS_FAIL) && (chip->errstat))
  472. status = chip->errstat(mtd, chip, FL_WRITING, status,
  473. page);
  474. if (status & NAND_STATUS_FAIL)
  475. return -EIO;
  476. } else {
  477. chip->cmdfunc(mtd, NAND_CMD_CACHEDPROG, -1, -1);
  478. status = chip->waitfunc(mtd, chip);
  479. }
  480. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  481. /* Send command to read back the data */
  482. chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
  483. if (chip->verify_buf(mtd, buf, mtd->writesize))
  484. return -EIO;
  485. #endif
  486. return 0;
  487. }
  488. static int cafe_nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
  489. {
  490. return 0;
  491. }
  492. /* F_2[X]/(X**6+X+1) */
  493. static unsigned short __devinit gf64_mul(u8 a, u8 b)
  494. {
  495. u8 c;
  496. unsigned int i;
  497. c = 0;
  498. for (i = 0; i < 6; i++) {
  499. if (a & 1)
  500. c ^= b;
  501. a >>= 1;
  502. b <<= 1;
  503. if ((b & 0x40) != 0)
  504. b ^= 0x43;
  505. }
  506. return c;
  507. }
  508. /* F_64[X]/(X**2+X+A**-1) with A the generator of F_64[X] */
  509. static u16 __devinit gf4096_mul(u16 a, u16 b)
  510. {
  511. u8 ah, al, bh, bl, ch, cl;
  512. ah = a >> 6;
  513. al = a & 0x3f;
  514. bh = b >> 6;
  515. bl = b & 0x3f;
  516. ch = gf64_mul(ah ^ al, bh ^ bl) ^ gf64_mul(al, bl);
  517. cl = gf64_mul(gf64_mul(ah, bh), 0x21) ^ gf64_mul(al, bl);
  518. return (ch << 6) ^ cl;
  519. }
  520. static int __devinit cafe_mul(int x)
  521. {
  522. if (x == 0)
  523. return 1;
  524. return gf4096_mul(x, 0xe01);
  525. }
  526. static int __devinit cafe_nand_probe(struct pci_dev *pdev,
  527. const struct pci_device_id *ent)
  528. {
  529. struct mtd_info *mtd;
  530. struct cafe_priv *cafe;
  531. uint32_t ctrl;
  532. int err = 0;
  533. #ifdef CONFIG_MTD_PARTITIONS
  534. struct mtd_partition *parts;
  535. int nr_parts;
  536. #endif
  537. /* Very old versions shared the same PCI ident for all three
  538. functions on the chip. Verify the class too... */
  539. if ((pdev->class >> 8) != PCI_CLASS_MEMORY_FLASH)
  540. return -ENODEV;
  541. err = pci_enable_device(pdev);
  542. if (err)
  543. return err;
  544. pci_set_master(pdev);
  545. mtd = kzalloc(sizeof(*mtd) + sizeof(struct cafe_priv), GFP_KERNEL);
  546. if (!mtd) {
  547. dev_warn(&pdev->dev, "failed to alloc mtd_info\n");
  548. return -ENOMEM;
  549. }
  550. cafe = (void *)(&mtd[1]);
  551. mtd->dev.parent = &pdev->dev;
  552. mtd->priv = cafe;
  553. mtd->owner = THIS_MODULE;
  554. cafe->pdev = pdev;
  555. cafe->mmio = pci_iomap(pdev, 0, 0);
  556. if (!cafe->mmio) {
  557. dev_warn(&pdev->dev, "failed to iomap\n");
  558. err = -ENOMEM;
  559. goto out_free_mtd;
  560. }
  561. cafe->dmabuf = dma_alloc_coherent(&cafe->pdev->dev, 2112 + sizeof(struct nand_buffers),
  562. &cafe->dmaaddr, GFP_KERNEL);
  563. if (!cafe->dmabuf) {
  564. err = -ENOMEM;
  565. goto out_ior;
  566. }
  567. cafe->nand.buffers = (void *)cafe->dmabuf + 2112;
  568. cafe->rs = init_rs_non_canonical(12, &cafe_mul, 0, 1, 8);
  569. if (!cafe->rs) {
  570. err = -ENOMEM;
  571. goto out_ior;
  572. }
  573. cafe->nand.cmdfunc = cafe_nand_cmdfunc;
  574. cafe->nand.dev_ready = cafe_device_ready;
  575. cafe->nand.read_byte = cafe_read_byte;
  576. cafe->nand.read_buf = cafe_read_buf;
  577. cafe->nand.write_buf = cafe_write_buf;
  578. cafe->nand.select_chip = cafe_select_chip;
  579. cafe->nand.chip_delay = 0;
  580. /* Enable the following for a flash based bad block table */
  581. cafe->nand.options = NAND_USE_FLASH_BBT | NAND_NO_AUTOINCR | NAND_OWN_BUFFERS;
  582. if (skipbbt) {
  583. cafe->nand.options |= NAND_SKIP_BBTSCAN;
  584. cafe->nand.block_bad = cafe_nand_block_bad;
  585. }
  586. if (numtimings && numtimings != 3) {
  587. dev_warn(&cafe->pdev->dev, "%d timing register values ignored; precisely three are required\n", numtimings);
  588. }
  589. if (numtimings == 3) {
  590. cafe_dev_dbg(&cafe->pdev->dev, "Using provided timings (%08x %08x %08x)\n",
  591. timing[0], timing[1], timing[2]);
  592. } else {
  593. timing[0] = cafe_readl(cafe, NAND_TIMING1);
  594. timing[1] = cafe_readl(cafe, NAND_TIMING2);
  595. timing[2] = cafe_readl(cafe, NAND_TIMING3);
  596. if (timing[0] | timing[1] | timing[2]) {
  597. cafe_dev_dbg(&cafe->pdev->dev, "Timing registers already set (%08x %08x %08x)\n",
  598. timing[0], timing[1], timing[2]);
  599. } else {
  600. dev_warn(&cafe->pdev->dev, "Timing registers unset; using most conservative defaults\n");
  601. timing[0] = timing[1] = timing[2] = 0xffffffff;
  602. }
  603. }
  604. /* Start off by resetting the NAND controller completely */
  605. cafe_writel(cafe, 1, NAND_RESET);
  606. cafe_writel(cafe, 0, NAND_RESET);
  607. cafe_writel(cafe, timing[0], NAND_TIMING1);
  608. cafe_writel(cafe, timing[1], NAND_TIMING2);
  609. cafe_writel(cafe, timing[2], NAND_TIMING3);
  610. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  611. err = request_irq(pdev->irq, &cafe_nand_interrupt, IRQF_SHARED,
  612. "CAFE NAND", mtd);
  613. if (err) {
  614. dev_warn(&pdev->dev, "Could not register IRQ %d\n", pdev->irq);
  615. goto out_free_dma;
  616. }
  617. /* Disable master reset, enable NAND clock */
  618. ctrl = cafe_readl(cafe, GLOBAL_CTRL);
  619. ctrl &= 0xffffeff0;
  620. ctrl |= 0x00007000;
  621. cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
  622. cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
  623. cafe_writel(cafe, 0, NAND_DMA_CTRL);
  624. cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
  625. cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
  626. /* Set up DMA address */
  627. cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
  628. if (sizeof(cafe->dmaaddr) > 4)
  629. /* Shift in two parts to shut the compiler up */
  630. cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
  631. else
  632. cafe_writel(cafe, 0, NAND_DMA_ADDR1);
  633. cafe_dev_dbg(&cafe->pdev->dev, "Set DMA address to %x (virt %p)\n",
  634. cafe_readl(cafe, NAND_DMA_ADDR0), cafe->dmabuf);
  635. /* Enable NAND IRQ in global IRQ mask register */
  636. cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
  637. cafe_dev_dbg(&cafe->pdev->dev, "Control %x, IRQ mask %x\n",
  638. cafe_readl(cafe, GLOBAL_CTRL), cafe_readl(cafe, GLOBAL_IRQ_MASK));
  639. /* Scan to find existence of the device */
  640. if (nand_scan_ident(mtd, 2, NULL)) {
  641. err = -ENXIO;
  642. goto out_irq;
  643. }
  644. cafe->ctl2 = 1<<27; /* Reed-Solomon ECC */
  645. if (mtd->writesize == 2048)
  646. cafe->ctl2 |= 1<<29; /* 2KiB page size */
  647. /* Set up ECC according to the type of chip we found */
  648. if (mtd->writesize == 2048) {
  649. cafe->nand.ecc.layout = &cafe_oobinfo_2048;
  650. cafe->nand.bbt_td = &cafe_bbt_main_descr_2048;
  651. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_2048;
  652. } else if (mtd->writesize == 512) {
  653. cafe->nand.ecc.layout = &cafe_oobinfo_512;
  654. cafe->nand.bbt_td = &cafe_bbt_main_descr_512;
  655. cafe->nand.bbt_md = &cafe_bbt_mirror_descr_512;
  656. } else {
  657. printk(KERN_WARNING "Unexpected NAND flash writesize %d. Aborting\n",
  658. mtd->writesize);
  659. goto out_irq;
  660. }
  661. cafe->nand.ecc.mode = NAND_ECC_HW_SYNDROME;
  662. cafe->nand.ecc.size = mtd->writesize;
  663. cafe->nand.ecc.bytes = 14;
  664. cafe->nand.ecc.hwctl = (void *)cafe_nand_bug;
  665. cafe->nand.ecc.calculate = (void *)cafe_nand_bug;
  666. cafe->nand.ecc.correct = (void *)cafe_nand_bug;
  667. cafe->nand.write_page = cafe_nand_write_page;
  668. cafe->nand.ecc.write_page = cafe_nand_write_page_lowlevel;
  669. cafe->nand.ecc.write_oob = cafe_nand_write_oob;
  670. cafe->nand.ecc.read_page = cafe_nand_read_page;
  671. cafe->nand.ecc.read_oob = cafe_nand_read_oob;
  672. err = nand_scan_tail(mtd);
  673. if (err)
  674. goto out_irq;
  675. pci_set_drvdata(pdev, mtd);
  676. /* We register the whole device first, separate from the partitions */
  677. add_mtd_device(mtd);
  678. #ifdef CONFIG_MTD_PARTITIONS
  679. #ifdef CONFIG_MTD_CMDLINE_PARTS
  680. mtd->name = "cafe_nand";
  681. #endif
  682. nr_parts = parse_mtd_partitions(mtd, part_probes, &parts, 0);
  683. if (nr_parts > 0) {
  684. cafe->parts = parts;
  685. dev_info(&cafe->pdev->dev, "%d partitions found\n", nr_parts);
  686. add_mtd_partitions(mtd, parts, nr_parts);
  687. }
  688. #endif
  689. goto out;
  690. out_irq:
  691. /* Disable NAND IRQ in global IRQ mask register */
  692. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  693. free_irq(pdev->irq, mtd);
  694. out_free_dma:
  695. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  696. out_ior:
  697. pci_iounmap(pdev, cafe->mmio);
  698. out_free_mtd:
  699. kfree(mtd);
  700. out:
  701. return err;
  702. }
  703. static void __devexit cafe_nand_remove(struct pci_dev *pdev)
  704. {
  705. struct mtd_info *mtd = pci_get_drvdata(pdev);
  706. struct cafe_priv *cafe = mtd->priv;
  707. del_mtd_device(mtd);
  708. /* Disable NAND IRQ in global IRQ mask register */
  709. cafe_writel(cafe, ~1 & cafe_readl(cafe, GLOBAL_IRQ_MASK), GLOBAL_IRQ_MASK);
  710. free_irq(pdev->irq, mtd);
  711. nand_release(mtd);
  712. free_rs(cafe->rs);
  713. pci_iounmap(pdev, cafe->mmio);
  714. dma_free_coherent(&cafe->pdev->dev, 2112, cafe->dmabuf, cafe->dmaaddr);
  715. kfree(mtd);
  716. }
  717. static const struct pci_device_id cafe_nand_tbl[] = {
  718. { PCI_VENDOR_ID_MARVELL, PCI_DEVICE_ID_MARVELL_88ALP01_NAND,
  719. PCI_ANY_ID, PCI_ANY_ID },
  720. { }
  721. };
  722. MODULE_DEVICE_TABLE(pci, cafe_nand_tbl);
  723. static int cafe_nand_resume(struct pci_dev *pdev)
  724. {
  725. uint32_t ctrl;
  726. struct mtd_info *mtd = pci_get_drvdata(pdev);
  727. struct cafe_priv *cafe = mtd->priv;
  728. /* Start off by resetting the NAND controller completely */
  729. cafe_writel(cafe, 1, NAND_RESET);
  730. cafe_writel(cafe, 0, NAND_RESET);
  731. cafe_writel(cafe, 0xffffffff, NAND_IRQ_MASK);
  732. /* Restore timing configuration */
  733. cafe_writel(cafe, timing[0], NAND_TIMING1);
  734. cafe_writel(cafe, timing[1], NAND_TIMING2);
  735. cafe_writel(cafe, timing[2], NAND_TIMING3);
  736. /* Disable master reset, enable NAND clock */
  737. ctrl = cafe_readl(cafe, GLOBAL_CTRL);
  738. ctrl &= 0xffffeff0;
  739. ctrl |= 0x00007000;
  740. cafe_writel(cafe, ctrl | 0x05, GLOBAL_CTRL);
  741. cafe_writel(cafe, ctrl | 0x0a, GLOBAL_CTRL);
  742. cafe_writel(cafe, 0, NAND_DMA_CTRL);
  743. cafe_writel(cafe, 0x7006, GLOBAL_CTRL);
  744. cafe_writel(cafe, 0x700a, GLOBAL_CTRL);
  745. /* Set up DMA address */
  746. cafe_writel(cafe, cafe->dmaaddr & 0xffffffff, NAND_DMA_ADDR0);
  747. if (sizeof(cafe->dmaaddr) > 4)
  748. /* Shift in two parts to shut the compiler up */
  749. cafe_writel(cafe, (cafe->dmaaddr >> 16) >> 16, NAND_DMA_ADDR1);
  750. else
  751. cafe_writel(cafe, 0, NAND_DMA_ADDR1);
  752. /* Enable NAND IRQ in global IRQ mask register */
  753. cafe_writel(cafe, 0x80000007, GLOBAL_IRQ_MASK);
  754. return 0;
  755. }
  756. static struct pci_driver cafe_nand_pci_driver = {
  757. .name = "CAFÉ NAND",
  758. .id_table = cafe_nand_tbl,
  759. .probe = cafe_nand_probe,
  760. .remove = __devexit_p(cafe_nand_remove),
  761. .resume = cafe_nand_resume,
  762. };
  763. static int __init cafe_nand_init(void)
  764. {
  765. return pci_register_driver(&cafe_nand_pci_driver);
  766. }
  767. static void __exit cafe_nand_exit(void)
  768. {
  769. pci_unregister_driver(&cafe_nand_pci_driver);
  770. }
  771. module_init(cafe_nand_init);
  772. module_exit(cafe_nand_exit);
  773. MODULE_LICENSE("GPL");
  774. MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
  775. MODULE_DESCRIPTION("NAND flash driver for OLPC CAFÉ chip");