at91_dataflash26.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Atmel DataFlash driver for Atmel AT91RM9200 (Thunder)
  3. * This is a largely modified version of at91_dataflash.c that
  4. * supports AT26xxx dataflash chips. The original driver supports
  5. * AT45xxx chips.
  6. *
  7. * Note: This driver was only tested with an AT26F004. It should be
  8. * easy to make it work with other AT26xxx dataflash devices, though.
  9. *
  10. * Copyright (C) 2007 Hans J. Koch <hjk@linutronix.de>
  11. * original Copyright (C) SAN People (Pty) Ltd
  12. *
  13. * This program is free software; you can redistribute it and/or
  14. * modify it under the terms of the GNU General Public License
  15. * version 2 as published by the Free Software Foundation.
  16. */
  17. #include <linux/config.h>
  18. #include <linux/module.h>
  19. #include <linux/init.h>
  20. #include <linux/mtd/mtd.h>
  21. #include <asm/arch/at91_spi.h>
  22. #define DATAFLASH_MAX_DEVICES 4 /* max number of dataflash devices */
  23. #define MANUFACTURER_ID_ATMEL 0x1F
  24. /* command codes */
  25. #define AT26_OP_READ_STATUS 0x05
  26. #define AT26_OP_READ_DEV_ID 0x9F
  27. #define AT26_OP_ERASE_PAGE_4K 0x20
  28. #define AT26_OP_READ_ARRAY_FAST 0x0B
  29. #define AT26_OP_SEQUENTIAL_WRITE 0xAF
  30. #define AT26_OP_WRITE_ENABLE 0x06
  31. #define AT26_OP_WRITE_DISABLE 0x04
  32. #define AT26_OP_SECTOR_PROTECT 0x36
  33. #define AT26_OP_SECTOR_UNPROTECT 0x39
  34. /* status register bits */
  35. #define AT26_STATUS_BUSY 0x01
  36. #define AT26_STATUS_WRITE_ENABLE 0x02
  37. struct dataflash_local
  38. {
  39. int spi; /* SPI chip-select number */
  40. unsigned int page_size; /* number of bytes per page */
  41. };
  42. /* Detected DataFlash devices */
  43. static struct mtd_info* mtd_devices[DATAFLASH_MAX_DEVICES];
  44. static int nr_devices = 0;
  45. /* Allocate a single SPI transfer descriptor. We're assuming that if multiple
  46. SPI transfers occur at the same time, spi_access_bus() will serialize them.
  47. If this is not valid, then either (i) each dataflash 'priv' structure
  48. needs it's own transfer descriptor, (ii) we lock this one, or (iii) use
  49. another mechanism. */
  50. static struct spi_transfer_list* spi_transfer_desc;
  51. /*
  52. * Perform a SPI transfer to access the DataFlash device.
  53. */
  54. static int do_spi_transfer(int nr, char* tx, int tx_len, char* rx, int rx_len,
  55. char* txnext, int txnext_len, char* rxnext, int rxnext_len)
  56. {
  57. struct spi_transfer_list* list = spi_transfer_desc;
  58. list->tx[0] = tx; list->txlen[0] = tx_len;
  59. list->rx[0] = rx; list->rxlen[0] = rx_len;
  60. list->tx[1] = txnext; list->txlen[1] = txnext_len;
  61. list->rx[1] = rxnext; list->rxlen[1] = rxnext_len;
  62. list->nr_transfers = nr;
  63. /* Note: spi_transfer() always returns 0, there are no error checks */
  64. return spi_transfer(list);
  65. }
  66. /*
  67. * Return the status of the DataFlash device.
  68. */
  69. static unsigned char at91_dataflash26_status(void)
  70. {
  71. unsigned char command[2];
  72. command[0] = AT26_OP_READ_STATUS;
  73. command[1] = 0;
  74. do_spi_transfer(1, command, 2, command, 2, NULL, 0, NULL, 0);
  75. return command[1];
  76. }
  77. /*
  78. * Poll the DataFlash device until it is READY.
  79. */
  80. static unsigned char at91_dataflash26_waitready(void)
  81. {
  82. unsigned char status;
  83. while (1) {
  84. status = at91_dataflash26_status();
  85. if (!(status & AT26_STATUS_BUSY))
  86. return status;
  87. }
  88. }
  89. /*
  90. * Enable/disable write access
  91. */
  92. static void at91_dataflash26_write_enable(int enable)
  93. {
  94. unsigned char cmd[2];
  95. DEBUG(MTD_DEBUG_LEVEL3, "write_enable: enable=%i\n", enable);
  96. if (enable)
  97. cmd[0] = AT26_OP_WRITE_ENABLE;
  98. else
  99. cmd[0] = AT26_OP_WRITE_DISABLE;
  100. cmd[1] = 0;
  101. do_spi_transfer(1, cmd, 2, cmd, 2, NULL, 0, NULL, 0);
  102. }
  103. /*
  104. * Protect/unprotect sector
  105. */
  106. static void at91_dataflash26_sector_protect(loff_t addr, int protect)
  107. {
  108. unsigned char cmd[4];
  109. DEBUG(MTD_DEBUG_LEVEL3, "sector_protect: addr=0x%06x prot=%d\n",
  110. addr, protect);
  111. if (protect)
  112. cmd[0] = AT26_OP_SECTOR_PROTECT;
  113. else
  114. cmd[0] = AT26_OP_SECTOR_UNPROTECT;
  115. cmd[1] = (addr & 0x00FF0000) >> 16;
  116. cmd[2] = (addr & 0x0000FF00) >> 8;
  117. cmd[3] = (addr & 0x000000FF);
  118. do_spi_transfer(1, cmd, 4, cmd, 4, NULL, 0, NULL, 0);
  119. }
  120. /*
  121. * Erase blocks of flash.
  122. */
  123. static int at91_dataflash26_erase(struct mtd_info *mtd,
  124. struct erase_info *instr)
  125. {
  126. struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
  127. unsigned char cmd[4];
  128. DEBUG(MTD_DEBUG_LEVEL1, "dataflash_erase: addr=0x%06x len=%i\n",
  129. instr->addr, instr->len);
  130. /* Sanity checks */
  131. if (priv->page_size != 4096)
  132. return -EINVAL; /* Can't handle other sizes at the moment */
  133. if ( ((instr->len % mtd->erasesize) != 0)
  134. || ((instr->len % priv->page_size) != 0)
  135. || ((instr->addr % priv->page_size) != 0)
  136. || ((instr->addr + instr->len) > mtd->size))
  137. return -EINVAL;
  138. spi_access_bus(priv->spi);
  139. while (instr->len > 0) {
  140. at91_dataflash26_write_enable(1);
  141. at91_dataflash26_sector_protect(instr->addr, 0);
  142. at91_dataflash26_write_enable(1);
  143. cmd[0] = AT26_OP_ERASE_PAGE_4K;
  144. cmd[1] = (instr->addr & 0x00FF0000) >> 16;
  145. cmd[2] = (instr->addr & 0x0000FF00) >> 8;
  146. cmd[3] = (instr->addr & 0x000000FF);
  147. DEBUG(MTD_DEBUG_LEVEL3, "ERASE: (0x%02x) 0x%02x 0x%02x"
  148. "0x%02x\n",
  149. cmd[0], cmd[1], cmd[2], cmd[3]);
  150. do_spi_transfer(1, cmd, 4, cmd, 4, NULL, 0, NULL, 0);
  151. at91_dataflash26_waitready();
  152. instr->addr += priv->page_size; /* next page */
  153. instr->len -= priv->page_size;
  154. }
  155. at91_dataflash26_write_enable(0);
  156. spi_release_bus(priv->spi);
  157. /* Inform MTD subsystem that erase is complete */
  158. instr->state = MTD_ERASE_DONE;
  159. if (instr->callback)
  160. instr->callback(instr);
  161. return 0;
  162. }
  163. /*
  164. * Read from the DataFlash device.
  165. * from : Start offset in flash device
  166. * len : Number of bytes to read
  167. * retlen : Number of bytes actually read
  168. * buf : Buffer that will receive data
  169. */
  170. static int at91_dataflash26_read(struct mtd_info *mtd, loff_t from, size_t len,
  171. size_t *retlen, u_char *buf)
  172. {
  173. struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
  174. unsigned char cmd[5];
  175. DEBUG(MTD_DEBUG_LEVEL1, "dataflash_read: %lli .. %lli\n",
  176. from, from+len);
  177. *retlen = 0;
  178. /* Sanity checks */
  179. if (!len)
  180. return 0;
  181. if (from + len > mtd->size)
  182. return -EINVAL;
  183. cmd[0] = AT26_OP_READ_ARRAY_FAST;
  184. cmd[1] = (from & 0x00FF0000) >> 16;
  185. cmd[2] = (from & 0x0000FF00) >> 8;
  186. cmd[3] = (from & 0x000000FF);
  187. /* cmd[4] is a "Don't care" byte */
  188. DEBUG(MTD_DEBUG_LEVEL3, "READ: (0x%02x) 0x%02x 0x%02x 0x%02x\n",
  189. cmd[0], cmd[1], cmd[2], cmd[3]);
  190. spi_access_bus(priv->spi);
  191. do_spi_transfer(2, cmd, 5, cmd, 5, buf, len, buf, len);
  192. spi_release_bus(priv->spi);
  193. *retlen = len;
  194. return 0;
  195. }
  196. /*
  197. * Write to the DataFlash device.
  198. * to : Start offset in flash device
  199. * len : Number of bytes to write
  200. * retlen : Number of bytes actually written
  201. * buf : Buffer containing the data
  202. */
  203. static int at91_dataflash26_write(struct mtd_info *mtd, loff_t to, size_t len,
  204. size_t *retlen, const u_char *buf)
  205. {
  206. struct dataflash_local *priv = (struct dataflash_local *) mtd->priv;
  207. unsigned int addr, buf_index = 0;
  208. int ret = -EIO, sector, last_sector;
  209. unsigned char status, cmd[5];
  210. DEBUG(MTD_DEBUG_LEVEL1, "dataflash_write: %lli .. %lli\n", to, to+len);
  211. *retlen = 0;
  212. /* Sanity checks */
  213. if (!len)
  214. return 0;
  215. if (to + len > mtd->size)
  216. return -EINVAL;
  217. spi_access_bus(priv->spi);
  218. addr = to;
  219. last_sector = -1;
  220. while (buf_index < len) {
  221. sector = addr / priv->page_size;
  222. /* Write first byte if a new sector begins */
  223. if (sector != last_sector) {
  224. at91_dataflash26_write_enable(1);
  225. at91_dataflash26_sector_protect(addr, 0);
  226. at91_dataflash26_write_enable(1);
  227. /* Program first byte of a new sector */
  228. cmd[0] = AT26_OP_SEQUENTIAL_WRITE;
  229. cmd[1] = (addr & 0x00FF0000) >> 16;
  230. cmd[2] = (addr & 0x0000FF00) >> 8;
  231. cmd[3] = (addr & 0x000000FF);
  232. cmd[4] = buf[buf_index++];
  233. do_spi_transfer(1, cmd, 5, cmd, 5, NULL, 0, NULL, 0);
  234. status = at91_dataflash26_waitready();
  235. addr++;
  236. /* On write errors, the chip resets the write enable
  237. flag. This also happens after the last byte of a
  238. sector is successfully programmed. */
  239. if ( ( !(status & AT26_STATUS_WRITE_ENABLE))
  240. && ((addr % priv->page_size) != 0) ) {
  241. DEBUG(MTD_DEBUG_LEVEL1,
  242. "write error1: addr=0x%06x, "
  243. "status=0x%02x\n", addr, status);
  244. goto write_err;
  245. }
  246. (*retlen)++;
  247. last_sector = sector;
  248. }
  249. /* Write subsequent bytes in the same sector */
  250. cmd[0] = AT26_OP_SEQUENTIAL_WRITE;
  251. cmd[1] = buf[buf_index++];
  252. do_spi_transfer(1, cmd, 2, cmd, 2, NULL, 0, NULL, 0);
  253. status = at91_dataflash26_waitready();
  254. addr++;
  255. if ( ( !(status & AT26_STATUS_WRITE_ENABLE))
  256. && ((addr % priv->page_size) != 0) ) {
  257. DEBUG(MTD_DEBUG_LEVEL1, "write error2: addr=0x%06x, "
  258. "status=0x%02x\n", addr, status);
  259. goto write_err;
  260. }
  261. (*retlen)++;
  262. }
  263. ret = 0;
  264. at91_dataflash26_write_enable(0);
  265. write_err:
  266. spi_release_bus(priv->spi);
  267. return ret;
  268. }
  269. /*
  270. * Initialize and register DataFlash device with MTD subsystem.
  271. */
  272. static int __init add_dataflash(int channel, char *name, int nr_pages,
  273. int pagesize)
  274. {
  275. struct mtd_info *device;
  276. struct dataflash_local *priv;
  277. if (nr_devices >= DATAFLASH_MAX_DEVICES) {
  278. printk(KERN_ERR "at91_dataflash26: Too many devices "
  279. "detected\n");
  280. return 0;
  281. }
  282. device = kzalloc(sizeof(struct mtd_info) + strlen(name) + 8,
  283. GFP_KERNEL);
  284. if (!device)
  285. return -ENOMEM;
  286. device->name = (char *)&device[1];
  287. sprintf(device->name, "%s.spi%d", name, channel);
  288. device->size = nr_pages * pagesize;
  289. device->erasesize = pagesize;
  290. device->owner = THIS_MODULE;
  291. device->type = MTD_DATAFLASH;
  292. device->flags = MTD_CAP_NORFLASH;
  293. device->erase = at91_dataflash26_erase;
  294. device->read = at91_dataflash26_read;
  295. device->write = at91_dataflash26_write;
  296. priv = (struct dataflash_local *)kzalloc(sizeof(struct dataflash_local),
  297. GFP_KERNEL);
  298. if (!priv) {
  299. kfree(device);
  300. return -ENOMEM;
  301. }
  302. priv->spi = channel;
  303. priv->page_size = pagesize;
  304. device->priv = priv;
  305. mtd_devices[nr_devices] = device;
  306. nr_devices++;
  307. printk(KERN_INFO "at91_dataflash26: %s detected [spi%i] (%i bytes)\n",
  308. name, channel, device->size);
  309. return add_mtd_device(device);
  310. }
  311. /*
  312. * Detect and initialize DataFlash device connected to specified SPI channel.
  313. *
  314. */
  315. struct dataflash26_types {
  316. unsigned char id0;
  317. unsigned char id1;
  318. char *name;
  319. int pagesize;
  320. int nr_pages;
  321. };
  322. struct dataflash26_types df26_types[] = {
  323. {
  324. .id0 = 0x04,
  325. .id1 = 0x00,
  326. .name = "AT26F004",
  327. .pagesize = 4096,
  328. .nr_pages = 128,
  329. },
  330. {
  331. .id0 = 0x45,
  332. .id1 = 0x01,
  333. .name = "AT26DF081A", /* Not tested ! */
  334. .pagesize = 4096,
  335. .nr_pages = 256,
  336. },
  337. };
  338. static int __init at91_dataflash26_detect(int channel)
  339. {
  340. unsigned char status, cmd[5];
  341. int i;
  342. spi_access_bus(channel);
  343. status = at91_dataflash26_status();
  344. if (status == 0 || status == 0xff) {
  345. printk(KERN_ERR "at91_dataflash26_detect: status error %d\n",
  346. status);
  347. spi_release_bus(channel);
  348. return -ENODEV;
  349. }
  350. cmd[0] = AT26_OP_READ_DEV_ID;
  351. do_spi_transfer(1, cmd, 5, cmd, 5, NULL, 0, NULL, 0);
  352. spi_release_bus(channel);
  353. if (cmd[1] != MANUFACTURER_ID_ATMEL)
  354. return -ENODEV;
  355. for (i = 0; i < ARRAY_SIZE(df26_types); i++) {
  356. if ( cmd[2] == df26_types[i].id0
  357. && cmd[3] == df26_types[i].id1)
  358. return add_dataflash(channel,
  359. df26_types[i].name,
  360. df26_types[i].nr_pages,
  361. df26_types[i].pagesize);
  362. }
  363. printk(KERN_ERR "at91_dataflash26_detect: Unsupported device "
  364. "(0x%02x/0x%02x)\n", cmd[2], cmd[3]);
  365. return -ENODEV;
  366. }
  367. static int __init at91_dataflash26_init(void)
  368. {
  369. spi_transfer_desc = kmalloc(sizeof(struct spi_transfer_list),
  370. GFP_KERNEL);
  371. if (!spi_transfer_desc)
  372. return -ENOMEM;
  373. /* DataFlash (SPI chip select 0) */
  374. at91_dataflash26_detect(0);
  375. #ifdef CONFIG_MTD_AT91_DATAFLASH_CARD
  376. /* DataFlash card (SPI chip select 3) */
  377. at91_dataflash26_detect(3);
  378. #endif
  379. return 0;
  380. }
  381. static void __exit at91_dataflash26_exit(void)
  382. {
  383. int i;
  384. for (i = 0; i < DATAFLASH_MAX_DEVICES; i++) {
  385. if (mtd_devices[i]) {
  386. del_mtd_device(mtd_devices[i]);
  387. kfree(mtd_devices[i]->priv);
  388. kfree(mtd_devices[i]);
  389. }
  390. }
  391. nr_devices = 0;
  392. kfree(spi_transfer_desc);
  393. }
  394. module_init(at91_dataflash26_init);
  395. module_exit(at91_dataflash26_exit);
  396. MODULE_LICENSE("GPL");
  397. MODULE_AUTHOR("Hans J. Koch");
  398. MODULE_DESCRIPTION("DataFlash AT26xxx driver for Atmel AT91RM9200");