spear_smi.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153
  1. /*
  2. * SMI (Serial Memory Controller) device driver for Serial NOR Flash on
  3. * SPEAr platform
  4. * The serial nor interface is largely based on drivers/mtd/m25p80.c,
  5. * however the SPI interface has been replaced by SMI.
  6. *
  7. * Copyright © 2010 STMicroelectronics.
  8. * Ashish Priyadarshi
  9. * Shiraz Hashim <shiraz.hashim@st.com>
  10. *
  11. * This file is licensed under the terms of the GNU General Public
  12. * License version 2. This program is licensed "as is" without any
  13. * warranty of any kind, whether express or implied.
  14. */
  15. #include <linux/clk.h>
  16. #include <linux/delay.h>
  17. #include <linux/device.h>
  18. #include <linux/err.h>
  19. #include <linux/errno.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/io.h>
  22. #include <linux/ioport.h>
  23. #include <linux/jiffies.h>
  24. #include <linux/kernel.h>
  25. #include <linux/module.h>
  26. #include <linux/param.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/pm.h>
  29. #include <linux/mtd/mtd.h>
  30. #include <linux/mtd/partitions.h>
  31. #include <linux/mtd/spear_smi.h>
  32. #include <linux/mutex.h>
  33. #include <linux/sched.h>
  34. #include <linux/slab.h>
  35. #include <linux/wait.h>
  36. #include <linux/of.h>
  37. #include <linux/of_address.h>
  38. /* SMI clock rate */
  39. #define SMI_MAX_CLOCK_FREQ 50000000 /* 50 MHz */
  40. /* MAX time out to safely come out of a erase or write busy conditions */
  41. #define SMI_PROBE_TIMEOUT (HZ / 10)
  42. #define SMI_MAX_TIME_OUT (3 * HZ)
  43. /* timeout for command completion */
  44. #define SMI_CMD_TIMEOUT (HZ / 10)
  45. /* registers of smi */
  46. #define SMI_CR1 0x0 /* SMI control register 1 */
  47. #define SMI_CR2 0x4 /* SMI control register 2 */
  48. #define SMI_SR 0x8 /* SMI status register */
  49. #define SMI_TR 0xC /* SMI transmit register */
  50. #define SMI_RR 0x10 /* SMI receive register */
  51. /* defines for control_reg 1 */
  52. #define BANK_EN (0xF << 0) /* enables all banks */
  53. #define DSEL_TIME (0x6 << 4) /* Deselect time 6 + 1 SMI_CK periods */
  54. #define SW_MODE (0x1 << 28) /* enables SW Mode */
  55. #define WB_MODE (0x1 << 29) /* Write Burst Mode */
  56. #define FAST_MODE (0x1 << 15) /* Fast Mode */
  57. #define HOLD1 (0x1 << 16) /* Clock Hold period selection */
  58. /* defines for control_reg 2 */
  59. #define SEND (0x1 << 7) /* Send data */
  60. #define TFIE (0x1 << 8) /* Transmission Flag Interrupt Enable */
  61. #define WCIE (0x1 << 9) /* Write Complete Interrupt Enable */
  62. #define RD_STATUS_REG (0x1 << 10) /* reads status reg */
  63. #define WE (0x1 << 11) /* Write Enable */
  64. #define TX_LEN_SHIFT 0
  65. #define RX_LEN_SHIFT 4
  66. #define BANK_SHIFT 12
  67. /* defines for status register */
  68. #define SR_WIP 0x1 /* Write in progress */
  69. #define SR_WEL 0x2 /* Write enable latch */
  70. #define SR_BP0 0x4 /* Block protect 0 */
  71. #define SR_BP1 0x8 /* Block protect 1 */
  72. #define SR_BP2 0x10 /* Block protect 2 */
  73. #define SR_SRWD 0x80 /* SR write protect */
  74. #define TFF 0x100 /* Transfer Finished Flag */
  75. #define WCF 0x200 /* Transfer Finished Flag */
  76. #define ERF1 0x400 /* Forbidden Write Request */
  77. #define ERF2 0x800 /* Forbidden Access */
  78. #define WM_SHIFT 12
  79. /* flash opcodes */
  80. #define OPCODE_RDID 0x9f /* Read JEDEC ID */
  81. /* Flash Device Ids maintenance section */
  82. /* data structure to maintain flash ids from different vendors */
  83. struct flash_device {
  84. char *name;
  85. u8 erase_cmd;
  86. u32 device_id;
  87. u32 pagesize;
  88. unsigned long sectorsize;
  89. unsigned long size_in_bytes;
  90. };
  91. #define FLASH_ID(n, es, id, psize, ssize, size) \
  92. { \
  93. .name = n, \
  94. .erase_cmd = es, \
  95. .device_id = id, \
  96. .pagesize = psize, \
  97. .sectorsize = ssize, \
  98. .size_in_bytes = size \
  99. }
  100. static struct flash_device flash_devices[] = {
  101. FLASH_ID("st m25p16" , 0xd8, 0x00152020, 0x100, 0x10000, 0x200000),
  102. FLASH_ID("st m25p32" , 0xd8, 0x00162020, 0x100, 0x10000, 0x400000),
  103. FLASH_ID("st m25p64" , 0xd8, 0x00172020, 0x100, 0x10000, 0x800000),
  104. FLASH_ID("st m25p128" , 0xd8, 0x00182020, 0x100, 0x40000, 0x1000000),
  105. FLASH_ID("st m25p05" , 0xd8, 0x00102020, 0x80 , 0x8000 , 0x10000),
  106. FLASH_ID("st m25p10" , 0xd8, 0x00112020, 0x80 , 0x8000 , 0x20000),
  107. FLASH_ID("st m25p20" , 0xd8, 0x00122020, 0x100, 0x10000, 0x40000),
  108. FLASH_ID("st m25p40" , 0xd8, 0x00132020, 0x100, 0x10000, 0x80000),
  109. FLASH_ID("st m25p80" , 0xd8, 0x00142020, 0x100, 0x10000, 0x100000),
  110. FLASH_ID("st m45pe10" , 0xd8, 0x00114020, 0x100, 0x10000, 0x20000),
  111. FLASH_ID("st m45pe20" , 0xd8, 0x00124020, 0x100, 0x10000, 0x40000),
  112. FLASH_ID("st m45pe40" , 0xd8, 0x00134020, 0x100, 0x10000, 0x80000),
  113. FLASH_ID("st m45pe80" , 0xd8, 0x00144020, 0x100, 0x10000, 0x100000),
  114. FLASH_ID("sp s25fl004" , 0xd8, 0x00120201, 0x100, 0x10000, 0x80000),
  115. FLASH_ID("sp s25fl008" , 0xd8, 0x00130201, 0x100, 0x10000, 0x100000),
  116. FLASH_ID("sp s25fl016" , 0xd8, 0x00140201, 0x100, 0x10000, 0x200000),
  117. FLASH_ID("sp s25fl032" , 0xd8, 0x00150201, 0x100, 0x10000, 0x400000),
  118. FLASH_ID("sp s25fl064" , 0xd8, 0x00160201, 0x100, 0x10000, 0x800000),
  119. FLASH_ID("atmel 25f512" , 0x52, 0x0065001F, 0x80 , 0x8000 , 0x10000),
  120. FLASH_ID("atmel 25f1024" , 0x52, 0x0060001F, 0x100, 0x8000 , 0x20000),
  121. FLASH_ID("atmel 25f2048" , 0x52, 0x0063001F, 0x100, 0x10000, 0x40000),
  122. FLASH_ID("atmel 25f4096" , 0x52, 0x0064001F, 0x100, 0x10000, 0x80000),
  123. FLASH_ID("atmel 25fs040" , 0xd7, 0x0004661F, 0x100, 0x10000, 0x80000),
  124. FLASH_ID("mac 25l512" , 0xd8, 0x001020C2, 0x010, 0x10000, 0x10000),
  125. FLASH_ID("mac 25l1005" , 0xd8, 0x001120C2, 0x010, 0x10000, 0x20000),
  126. FLASH_ID("mac 25l2005" , 0xd8, 0x001220C2, 0x010, 0x10000, 0x40000),
  127. FLASH_ID("mac 25l4005" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  128. FLASH_ID("mac 25l4005a" , 0xd8, 0x001320C2, 0x010, 0x10000, 0x80000),
  129. FLASH_ID("mac 25l8005" , 0xd8, 0x001420C2, 0x010, 0x10000, 0x100000),
  130. FLASH_ID("mac 25l1605" , 0xd8, 0x001520C2, 0x100, 0x10000, 0x200000),
  131. FLASH_ID("mac 25l1605a" , 0xd8, 0x001520C2, 0x010, 0x10000, 0x200000),
  132. FLASH_ID("mac 25l3205" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  133. FLASH_ID("mac 25l3205a" , 0xd8, 0x001620C2, 0x100, 0x10000, 0x400000),
  134. FLASH_ID("mac 25l6405" , 0xd8, 0x001720C2, 0x100, 0x10000, 0x800000),
  135. };
  136. /* Define spear specific structures */
  137. struct spear_snor_flash;
  138. /**
  139. * struct spear_smi - Structure for SMI Device
  140. *
  141. * @clk: functional clock
  142. * @status: current status register of SMI.
  143. * @clk_rate: functional clock rate of SMI (default: SMI_MAX_CLOCK_FREQ)
  144. * @lock: lock to prevent parallel access of SMI.
  145. * @io_base: base address for registers of SMI.
  146. * @pdev: platform device
  147. * @cmd_complete: queue to wait for command completion of NOR-flash.
  148. * @num_flashes: number of flashes actually present on board.
  149. * @flash: separate structure for each Serial NOR-flash attached to SMI.
  150. */
  151. struct spear_smi {
  152. struct clk *clk;
  153. u32 status;
  154. unsigned long clk_rate;
  155. struct mutex lock;
  156. void __iomem *io_base;
  157. struct platform_device *pdev;
  158. wait_queue_head_t cmd_complete;
  159. u32 num_flashes;
  160. struct spear_snor_flash *flash[MAX_NUM_FLASH_CHIP];
  161. };
  162. /**
  163. * struct spear_snor_flash - Structure for Serial NOR Flash
  164. *
  165. * @bank: Bank number(0, 1, 2, 3) for each NOR-flash.
  166. * @dev_id: Device ID of NOR-flash.
  167. * @lock: lock to manage flash read, write and erase operations
  168. * @mtd: MTD info for each NOR-flash.
  169. * @num_parts: Total number of partition in each bank of NOR-flash.
  170. * @parts: Partition info for each bank of NOR-flash.
  171. * @page_size: Page size of NOR-flash.
  172. * @base_addr: Base address of NOR-flash.
  173. * @erase_cmd: erase command may vary on different flash types
  174. * @fast_mode: flash supports read in fast mode
  175. */
  176. struct spear_snor_flash {
  177. u32 bank;
  178. u32 dev_id;
  179. struct mutex lock;
  180. struct mtd_info mtd;
  181. u32 num_parts;
  182. struct mtd_partition *parts;
  183. u32 page_size;
  184. void __iomem *base_addr;
  185. u8 erase_cmd;
  186. u8 fast_mode;
  187. };
  188. static inline struct spear_snor_flash *get_flash_data(struct mtd_info *mtd)
  189. {
  190. return container_of(mtd, struct spear_snor_flash, mtd);
  191. }
  192. /**
  193. * spear_smi_read_sr - Read status register of flash through SMI
  194. * @dev: structure of SMI information.
  195. * @bank: bank to which flash is connected
  196. *
  197. * This routine will return the status register of the flash chip present at the
  198. * given bank.
  199. */
  200. static int spear_smi_read_sr(struct spear_smi *dev, u32 bank)
  201. {
  202. int ret;
  203. u32 ctrlreg1;
  204. mutex_lock(&dev->lock);
  205. dev->status = 0; /* Will be set in interrupt handler */
  206. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  207. /* program smi in hw mode */
  208. writel(ctrlreg1 & ~(SW_MODE | WB_MODE), dev->io_base + SMI_CR1);
  209. /* performing a rsr instruction in hw mode */
  210. writel((bank << BANK_SHIFT) | RD_STATUS_REG | TFIE,
  211. dev->io_base + SMI_CR2);
  212. /* wait for tff */
  213. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  214. dev->status & TFF, SMI_CMD_TIMEOUT);
  215. /* copy dev->status (lower 16 bits) in order to release lock */
  216. if (ret > 0)
  217. ret = dev->status & 0xffff;
  218. else
  219. ret = -EIO;
  220. /* restore the ctrl regs state */
  221. writel(ctrlreg1, dev->io_base + SMI_CR1);
  222. writel(0, dev->io_base + SMI_CR2);
  223. mutex_unlock(&dev->lock);
  224. return ret;
  225. }
  226. /**
  227. * spear_smi_wait_till_ready - wait till flash is ready
  228. * @dev: structure of SMI information.
  229. * @bank: flash corresponding to this bank
  230. * @timeout: timeout for busy wait condition
  231. *
  232. * This routine checks for WIP (write in progress) bit in Status register
  233. * If successful the routine returns 0 else -EBUSY
  234. */
  235. static int spear_smi_wait_till_ready(struct spear_smi *dev, u32 bank,
  236. unsigned long timeout)
  237. {
  238. unsigned long finish;
  239. int status;
  240. finish = jiffies + timeout;
  241. do {
  242. status = spear_smi_read_sr(dev, bank);
  243. if (status < 0)
  244. continue; /* try till timeout */
  245. else if (!(status & SR_WIP))
  246. return 0;
  247. cond_resched();
  248. } while (!time_after_eq(jiffies, finish));
  249. dev_err(&dev->pdev->dev, "smi controller is busy, timeout\n");
  250. return status;
  251. }
  252. /**
  253. * spear_smi_int_handler - SMI Interrupt Handler.
  254. * @irq: irq number
  255. * @dev_id: structure of SMI device, embedded in dev_id.
  256. *
  257. * The handler clears all interrupt conditions and records the status in
  258. * dev->status which is used by the driver later.
  259. */
  260. static irqreturn_t spear_smi_int_handler(int irq, void *dev_id)
  261. {
  262. u32 status = 0;
  263. struct spear_smi *dev = dev_id;
  264. status = readl(dev->io_base + SMI_SR);
  265. if (unlikely(!status))
  266. return IRQ_NONE;
  267. /* clear all interrupt conditions */
  268. writel(0, dev->io_base + SMI_SR);
  269. /* copy the status register in dev->status */
  270. dev->status |= status;
  271. /* send the completion */
  272. wake_up_interruptible(&dev->cmd_complete);
  273. return IRQ_HANDLED;
  274. }
  275. /**
  276. * spear_smi_hw_init - initializes the smi controller.
  277. * @dev: structure of smi device
  278. *
  279. * this routine initializes the smi controller wit the default values
  280. */
  281. static void spear_smi_hw_init(struct spear_smi *dev)
  282. {
  283. unsigned long rate = 0;
  284. u32 prescale = 0;
  285. u32 val;
  286. rate = clk_get_rate(dev->clk);
  287. /* functional clock of smi */
  288. prescale = DIV_ROUND_UP(rate, dev->clk_rate);
  289. /*
  290. * setting the standard values, fast mode, prescaler for
  291. * SMI_MAX_CLOCK_FREQ (50MHz) operation and bank enable
  292. */
  293. val = HOLD1 | BANK_EN | DSEL_TIME | (prescale << 8);
  294. mutex_lock(&dev->lock);
  295. writel(val, dev->io_base + SMI_CR1);
  296. mutex_unlock(&dev->lock);
  297. }
  298. /**
  299. * get_flash_index - match chip id from a flash list.
  300. * @flash_id: a valid nor flash chip id obtained from board.
  301. *
  302. * try to validate the chip id by matching from a list, if not found then simply
  303. * returns negative. In case of success returns index in to the flash devices
  304. * array.
  305. */
  306. static int get_flash_index(u32 flash_id)
  307. {
  308. int index;
  309. /* Matches chip-id to entire list of 'serial-nor flash' ids */
  310. for (index = 0; index < ARRAY_SIZE(flash_devices); index++) {
  311. if (flash_devices[index].device_id == flash_id)
  312. return index;
  313. }
  314. /* Memory chip is not listed and not supported */
  315. return -ENODEV;
  316. }
  317. /**
  318. * spear_smi_write_enable - Enable the flash to do write operation
  319. * @dev: structure of SMI device
  320. * @bank: enable write for flash connected to this bank
  321. *
  322. * Set write enable latch with Write Enable command.
  323. * Returns 0 on success.
  324. */
  325. static int spear_smi_write_enable(struct spear_smi *dev, u32 bank)
  326. {
  327. int ret;
  328. u32 ctrlreg1;
  329. mutex_lock(&dev->lock);
  330. dev->status = 0; /* Will be set in interrupt handler */
  331. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  332. /* program smi in h/w mode */
  333. writel(ctrlreg1 & ~SW_MODE, dev->io_base + SMI_CR1);
  334. /* give the flash, write enable command */
  335. writel((bank << BANK_SHIFT) | WE | TFIE, dev->io_base + SMI_CR2);
  336. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  337. dev->status & TFF, SMI_CMD_TIMEOUT);
  338. /* restore the ctrl regs state */
  339. writel(ctrlreg1, dev->io_base + SMI_CR1);
  340. writel(0, dev->io_base + SMI_CR2);
  341. if (ret <= 0) {
  342. ret = -EIO;
  343. dev_err(&dev->pdev->dev,
  344. "smi controller failed on write enable\n");
  345. } else {
  346. /* check whether write mode status is set for required bank */
  347. if (dev->status & (1 << (bank + WM_SHIFT)))
  348. ret = 0;
  349. else {
  350. dev_err(&dev->pdev->dev, "couldn't enable write\n");
  351. ret = -EIO;
  352. }
  353. }
  354. mutex_unlock(&dev->lock);
  355. return ret;
  356. }
  357. static inline u32
  358. get_sector_erase_cmd(struct spear_snor_flash *flash, u32 offset)
  359. {
  360. u32 cmd;
  361. u8 *x = (u8 *)&cmd;
  362. x[0] = flash->erase_cmd;
  363. x[1] = offset >> 16;
  364. x[2] = offset >> 8;
  365. x[3] = offset;
  366. return cmd;
  367. }
  368. /**
  369. * spear_smi_erase_sector - erase one sector of flash
  370. * @dev: structure of SMI information
  371. * @command: erase command to be send
  372. * @bank: bank to which this command needs to be send
  373. * @bytes: size of command
  374. *
  375. * Erase one sector of flash memory at offset ``offset'' which is any
  376. * address within the sector which should be erased.
  377. * Returns 0 if successful, non-zero otherwise.
  378. */
  379. static int spear_smi_erase_sector(struct spear_smi *dev,
  380. u32 bank, u32 command, u32 bytes)
  381. {
  382. u32 ctrlreg1 = 0;
  383. int ret;
  384. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  385. if (ret)
  386. return ret;
  387. ret = spear_smi_write_enable(dev, bank);
  388. if (ret)
  389. return ret;
  390. mutex_lock(&dev->lock);
  391. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  392. writel((ctrlreg1 | SW_MODE) & ~WB_MODE, dev->io_base + SMI_CR1);
  393. /* send command in sw mode */
  394. writel(command, dev->io_base + SMI_TR);
  395. writel((bank << BANK_SHIFT) | SEND | TFIE | (bytes << TX_LEN_SHIFT),
  396. dev->io_base + SMI_CR2);
  397. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  398. dev->status & TFF, SMI_CMD_TIMEOUT);
  399. if (ret <= 0) {
  400. ret = -EIO;
  401. dev_err(&dev->pdev->dev, "sector erase failed\n");
  402. } else
  403. ret = 0; /* success */
  404. /* restore ctrl regs */
  405. writel(ctrlreg1, dev->io_base + SMI_CR1);
  406. writel(0, dev->io_base + SMI_CR2);
  407. mutex_unlock(&dev->lock);
  408. return ret;
  409. }
  410. /**
  411. * spear_mtd_erase - perform flash erase operation as requested by user
  412. * @mtd: Provides the memory characteristics
  413. * @e_info: Provides the erase information
  414. *
  415. * Erase an address range on the flash chip. The address range may extend
  416. * one or more erase sectors. Return an error is there is a problem erasing.
  417. */
  418. static int spear_mtd_erase(struct mtd_info *mtd, struct erase_info *e_info)
  419. {
  420. struct spear_snor_flash *flash = get_flash_data(mtd);
  421. struct spear_smi *dev = mtd->priv;
  422. u32 addr, command, bank;
  423. int len, ret;
  424. if (!flash || !dev)
  425. return -ENODEV;
  426. bank = flash->bank;
  427. if (bank > dev->num_flashes - 1) {
  428. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  429. return -EINVAL;
  430. }
  431. addr = e_info->addr;
  432. len = e_info->len;
  433. mutex_lock(&flash->lock);
  434. /* now erase sectors in loop */
  435. while (len) {
  436. command = get_sector_erase_cmd(flash, addr);
  437. /* preparing the command for flash */
  438. ret = spear_smi_erase_sector(dev, bank, command, 4);
  439. if (ret) {
  440. e_info->state = MTD_ERASE_FAILED;
  441. mutex_unlock(&flash->lock);
  442. return ret;
  443. }
  444. addr += mtd->erasesize;
  445. len -= mtd->erasesize;
  446. }
  447. mutex_unlock(&flash->lock);
  448. e_info->state = MTD_ERASE_DONE;
  449. mtd_erase_callback(e_info);
  450. return 0;
  451. }
  452. /**
  453. * spear_mtd_read - performs flash read operation as requested by the user
  454. * @mtd: MTD information of the memory bank
  455. * @from: Address from which to start read
  456. * @len: Number of bytes to be read
  457. * @retlen: Fills the Number of bytes actually read
  458. * @buf: Fills this after reading
  459. *
  460. * Read an address range from the flash chip. The address range
  461. * may be any size provided it is within the physical boundaries.
  462. * Returns 0 on success, non zero otherwise
  463. */
  464. static int spear_mtd_read(struct mtd_info *mtd, loff_t from, size_t len,
  465. size_t *retlen, u8 *buf)
  466. {
  467. struct spear_snor_flash *flash = get_flash_data(mtd);
  468. struct spear_smi *dev = mtd->priv;
  469. void *src;
  470. u32 ctrlreg1, val;
  471. int ret;
  472. if (!flash || !dev)
  473. return -ENODEV;
  474. if (flash->bank > dev->num_flashes - 1) {
  475. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  476. return -EINVAL;
  477. }
  478. /* select address as per bank number */
  479. src = flash->base_addr + from;
  480. mutex_lock(&flash->lock);
  481. /* wait till previous write/erase is done. */
  482. ret = spear_smi_wait_till_ready(dev, flash->bank, SMI_MAX_TIME_OUT);
  483. if (ret) {
  484. mutex_unlock(&flash->lock);
  485. return ret;
  486. }
  487. mutex_lock(&dev->lock);
  488. /* put smi in hw mode not wbt mode */
  489. ctrlreg1 = val = readl(dev->io_base + SMI_CR1);
  490. val &= ~(SW_MODE | WB_MODE);
  491. if (flash->fast_mode)
  492. val |= FAST_MODE;
  493. writel(val, dev->io_base + SMI_CR1);
  494. memcpy_fromio(buf, (u8 *)src, len);
  495. /* restore ctrl reg1 */
  496. writel(ctrlreg1, dev->io_base + SMI_CR1);
  497. mutex_unlock(&dev->lock);
  498. *retlen = len;
  499. mutex_unlock(&flash->lock);
  500. return 0;
  501. }
  502. static inline int spear_smi_cpy_toio(struct spear_smi *dev, u32 bank,
  503. void *dest, const void *src, size_t len)
  504. {
  505. int ret;
  506. u32 ctrlreg1;
  507. /* wait until finished previous write command. */
  508. ret = spear_smi_wait_till_ready(dev, bank, SMI_MAX_TIME_OUT);
  509. if (ret)
  510. return ret;
  511. /* put smi in write enable */
  512. ret = spear_smi_write_enable(dev, bank);
  513. if (ret)
  514. return ret;
  515. /* put smi in hw, write burst mode */
  516. mutex_lock(&dev->lock);
  517. ctrlreg1 = readl(dev->io_base + SMI_CR1);
  518. writel((ctrlreg1 | WB_MODE) & ~SW_MODE, dev->io_base + SMI_CR1);
  519. memcpy_toio(dest, src, len);
  520. writel(ctrlreg1, dev->io_base + SMI_CR1);
  521. mutex_unlock(&dev->lock);
  522. return 0;
  523. }
  524. /**
  525. * spear_mtd_write - performs write operation as requested by the user.
  526. * @mtd: MTD information of the memory bank.
  527. * @to: Address to write.
  528. * @len: Number of bytes to be written.
  529. * @retlen: Number of bytes actually wrote.
  530. * @buf: Buffer from which the data to be taken.
  531. *
  532. * Write an address range to the flash chip. Data must be written in
  533. * flash_page_size chunks. The address range may be any size provided
  534. * it is within the physical boundaries.
  535. * Returns 0 on success, non zero otherwise
  536. */
  537. static int spear_mtd_write(struct mtd_info *mtd, loff_t to, size_t len,
  538. size_t *retlen, const u8 *buf)
  539. {
  540. struct spear_snor_flash *flash = get_flash_data(mtd);
  541. struct spear_smi *dev = mtd->priv;
  542. void *dest;
  543. u32 page_offset, page_size;
  544. int ret;
  545. if (!flash || !dev)
  546. return -ENODEV;
  547. if (flash->bank > dev->num_flashes - 1) {
  548. dev_err(&dev->pdev->dev, "Invalid Bank Num");
  549. return -EINVAL;
  550. }
  551. /* select address as per bank number */
  552. dest = flash->base_addr + to;
  553. mutex_lock(&flash->lock);
  554. page_offset = (u32)to % flash->page_size;
  555. /* do if all the bytes fit onto one page */
  556. if (page_offset + len <= flash->page_size) {
  557. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf, len);
  558. if (!ret)
  559. *retlen += len;
  560. } else {
  561. u32 i;
  562. /* the size of data remaining on the first page */
  563. page_size = flash->page_size - page_offset;
  564. ret = spear_smi_cpy_toio(dev, flash->bank, dest, buf,
  565. page_size);
  566. if (ret)
  567. goto err_write;
  568. else
  569. *retlen += page_size;
  570. /* write everything in pagesize chunks */
  571. for (i = page_size; i < len; i += page_size) {
  572. page_size = len - i;
  573. if (page_size > flash->page_size)
  574. page_size = flash->page_size;
  575. ret = spear_smi_cpy_toio(dev, flash->bank, dest + i,
  576. buf + i, page_size);
  577. if (ret)
  578. break;
  579. else
  580. *retlen += page_size;
  581. }
  582. }
  583. err_write:
  584. mutex_unlock(&flash->lock);
  585. return ret;
  586. }
  587. /**
  588. * spear_smi_probe_flash - Detects the NOR Flash chip.
  589. * @dev: structure of SMI information.
  590. * @bank: bank on which flash must be probed
  591. *
  592. * This routine will check whether there exists a flash chip on a given memory
  593. * bank ID.
  594. * Return index of the probed flash in flash devices structure
  595. */
  596. static int spear_smi_probe_flash(struct spear_smi *dev, u32 bank)
  597. {
  598. int ret;
  599. u32 val = 0;
  600. ret = spear_smi_wait_till_ready(dev, bank, SMI_PROBE_TIMEOUT);
  601. if (ret)
  602. return ret;
  603. mutex_lock(&dev->lock);
  604. dev->status = 0; /* Will be set in interrupt handler */
  605. /* put smi in sw mode */
  606. val = readl(dev->io_base + SMI_CR1);
  607. writel(val | SW_MODE, dev->io_base + SMI_CR1);
  608. /* send readid command in sw mode */
  609. writel(OPCODE_RDID, dev->io_base + SMI_TR);
  610. val = (bank << BANK_SHIFT) | SEND | (1 << TX_LEN_SHIFT) |
  611. (3 << RX_LEN_SHIFT) | TFIE;
  612. writel(val, dev->io_base + SMI_CR2);
  613. /* wait for TFF */
  614. ret = wait_event_interruptible_timeout(dev->cmd_complete,
  615. dev->status & TFF, SMI_CMD_TIMEOUT);
  616. if (ret <= 0) {
  617. ret = -ENODEV;
  618. goto err_probe;
  619. }
  620. /* get memory chip id */
  621. val = readl(dev->io_base + SMI_RR);
  622. val &= 0x00ffffff;
  623. ret = get_flash_index(val);
  624. err_probe:
  625. /* clear sw mode */
  626. val = readl(dev->io_base + SMI_CR1);
  627. writel(val & ~SW_MODE, dev->io_base + SMI_CR1);
  628. mutex_unlock(&dev->lock);
  629. return ret;
  630. }
  631. #ifdef CONFIG_OF
  632. static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
  633. struct device_node *np)
  634. {
  635. struct spear_smi_plat_data *pdata = dev_get_platdata(&pdev->dev);
  636. struct device_node *pp = NULL;
  637. const __be32 *addr;
  638. u32 val;
  639. int len;
  640. int i = 0;
  641. if (!np)
  642. return -ENODEV;
  643. of_property_read_u32(np, "clock-rate", &val);
  644. pdata->clk_rate = val;
  645. pdata->board_flash_info = devm_kzalloc(&pdev->dev,
  646. sizeof(*pdata->board_flash_info),
  647. GFP_KERNEL);
  648. /* Fill structs for each subnode (flash device) */
  649. while ((pp = of_get_next_child(np, pp))) {
  650. struct spear_smi_flash_info *flash_info;
  651. flash_info = &pdata->board_flash_info[i];
  652. pdata->np[i] = pp;
  653. /* Read base-addr and size from DT */
  654. addr = of_get_property(pp, "reg", &len);
  655. pdata->board_flash_info->mem_base = be32_to_cpup(&addr[0]);
  656. pdata->board_flash_info->size = be32_to_cpup(&addr[1]);
  657. if (of_get_property(pp, "st,smi-fast-mode", NULL))
  658. pdata->board_flash_info->fast_mode = 1;
  659. i++;
  660. }
  661. pdata->num_flashes = i;
  662. return 0;
  663. }
  664. #else
  665. static int __devinit spear_smi_probe_config_dt(struct platform_device *pdev,
  666. struct device_node *np)
  667. {
  668. return -ENOSYS;
  669. }
  670. #endif
  671. static int spear_smi_setup_banks(struct platform_device *pdev,
  672. u32 bank, struct device_node *np)
  673. {
  674. struct spear_smi *dev = platform_get_drvdata(pdev);
  675. struct mtd_part_parser_data ppdata = {};
  676. struct spear_smi_flash_info *flash_info;
  677. struct spear_smi_plat_data *pdata;
  678. struct spear_snor_flash *flash;
  679. struct mtd_partition *parts = NULL;
  680. int count = 0;
  681. int flash_index;
  682. int ret = 0;
  683. pdata = dev_get_platdata(&pdev->dev);
  684. if (bank > pdata->num_flashes - 1)
  685. return -EINVAL;
  686. flash_info = &pdata->board_flash_info[bank];
  687. if (!flash_info)
  688. return -ENODEV;
  689. flash = kzalloc(sizeof(*flash), GFP_ATOMIC);
  690. if (!flash)
  691. return -ENOMEM;
  692. flash->bank = bank;
  693. flash->fast_mode = flash_info->fast_mode ? 1 : 0;
  694. mutex_init(&flash->lock);
  695. /* verify whether nor flash is really present on board */
  696. flash_index = spear_smi_probe_flash(dev, bank);
  697. if (flash_index < 0) {
  698. dev_info(&dev->pdev->dev, "smi-nor%d not found\n", bank);
  699. ret = flash_index;
  700. goto err_probe;
  701. }
  702. /* map the memory for nor flash chip */
  703. flash->base_addr = ioremap(flash_info->mem_base, flash_info->size);
  704. if (!flash->base_addr) {
  705. ret = -EIO;
  706. goto err_probe;
  707. }
  708. dev->flash[bank] = flash;
  709. flash->mtd.priv = dev;
  710. if (flash_info->name)
  711. flash->mtd.name = flash_info->name;
  712. else
  713. flash->mtd.name = flash_devices[flash_index].name;
  714. flash->mtd.type = MTD_NORFLASH;
  715. flash->mtd.writesize = 1;
  716. flash->mtd.flags = MTD_CAP_NORFLASH;
  717. flash->mtd.size = flash_info->size;
  718. flash->mtd.erasesize = flash_devices[flash_index].sectorsize;
  719. flash->page_size = flash_devices[flash_index].pagesize;
  720. flash->mtd.writebufsize = flash->page_size;
  721. flash->erase_cmd = flash_devices[flash_index].erase_cmd;
  722. flash->mtd._erase = spear_mtd_erase;
  723. flash->mtd._read = spear_mtd_read;
  724. flash->mtd._write = spear_mtd_write;
  725. flash->dev_id = flash_devices[flash_index].device_id;
  726. dev_info(&dev->pdev->dev, "mtd .name=%s .size=%llx(%lluM)\n",
  727. flash->mtd.name, flash->mtd.size,
  728. flash->mtd.size / (1024 * 1024));
  729. dev_info(&dev->pdev->dev, ".erasesize = 0x%x(%uK)\n",
  730. flash->mtd.erasesize, flash->mtd.erasesize / 1024);
  731. #ifndef CONFIG_OF
  732. if (flash_info->partitions) {
  733. parts = flash_info->partitions;
  734. count = flash_info->nr_partitions;
  735. }
  736. #endif
  737. ppdata.of_node = np;
  738. ret = mtd_device_parse_register(&flash->mtd, NULL, &ppdata, parts,
  739. count);
  740. if (ret) {
  741. dev_err(&dev->pdev->dev, "Err MTD partition=%d\n", ret);
  742. goto err_map;
  743. }
  744. return 0;
  745. err_map:
  746. iounmap(flash->base_addr);
  747. err_probe:
  748. kfree(flash);
  749. return ret;
  750. }
  751. /**
  752. * spear_smi_probe - Entry routine
  753. * @pdev: platform device structure
  754. *
  755. * This is the first routine which gets invoked during booting and does all
  756. * initialization/allocation work. The routine looks for available memory banks,
  757. * and do proper init for any found one.
  758. * Returns 0 on success, non zero otherwise
  759. */
  760. static int __devinit spear_smi_probe(struct platform_device *pdev)
  761. {
  762. struct device_node *np = pdev->dev.of_node;
  763. struct spear_smi_plat_data *pdata = NULL;
  764. struct spear_smi *dev;
  765. struct resource *smi_base;
  766. int irq, ret = 0;
  767. int i;
  768. if (np) {
  769. pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
  770. if (!pdata) {
  771. pr_err("%s: ERROR: no memory", __func__);
  772. ret = -ENOMEM;
  773. goto err;
  774. }
  775. pdev->dev.platform_data = pdata;
  776. ret = spear_smi_probe_config_dt(pdev, np);
  777. if (ret) {
  778. ret = -ENODEV;
  779. dev_err(&pdev->dev, "no platform data\n");
  780. goto err;
  781. }
  782. } else {
  783. pdata = dev_get_platdata(&pdev->dev);
  784. if (pdata < 0) {
  785. ret = -ENODEV;
  786. dev_err(&pdev->dev, "no platform data\n");
  787. goto err;
  788. }
  789. }
  790. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  791. if (!smi_base) {
  792. ret = -ENODEV;
  793. dev_err(&pdev->dev, "invalid smi base address\n");
  794. goto err;
  795. }
  796. irq = platform_get_irq(pdev, 0);
  797. if (irq < 0) {
  798. ret = -ENODEV;
  799. dev_err(&pdev->dev, "invalid smi irq\n");
  800. goto err;
  801. }
  802. dev = kzalloc(sizeof(*dev), GFP_ATOMIC);
  803. if (!dev) {
  804. ret = -ENOMEM;
  805. dev_err(&pdev->dev, "mem alloc fail\n");
  806. goto err;
  807. }
  808. smi_base = request_mem_region(smi_base->start, resource_size(smi_base),
  809. pdev->name);
  810. if (!smi_base) {
  811. ret = -EBUSY;
  812. dev_err(&pdev->dev, "request mem region fail\n");
  813. goto err_mem;
  814. }
  815. dev->io_base = ioremap(smi_base->start, resource_size(smi_base));
  816. if (!dev->io_base) {
  817. ret = -EIO;
  818. dev_err(&pdev->dev, "ioremap fail\n");
  819. goto err_ioremap;
  820. }
  821. dev->pdev = pdev;
  822. dev->clk_rate = pdata->clk_rate;
  823. if (dev->clk_rate < 0 || dev->clk_rate > SMI_MAX_CLOCK_FREQ)
  824. dev->clk_rate = SMI_MAX_CLOCK_FREQ;
  825. dev->num_flashes = pdata->num_flashes;
  826. if (dev->num_flashes > MAX_NUM_FLASH_CHIP) {
  827. dev_err(&pdev->dev, "exceeding max number of flashes\n");
  828. dev->num_flashes = MAX_NUM_FLASH_CHIP;
  829. }
  830. dev->clk = clk_get(&pdev->dev, NULL);
  831. if (IS_ERR(dev->clk)) {
  832. ret = PTR_ERR(dev->clk);
  833. goto err_clk;
  834. }
  835. ret = clk_prepare_enable(dev->clk);
  836. if (ret)
  837. goto err_clk_prepare_enable;
  838. ret = request_irq(irq, spear_smi_int_handler, 0, pdev->name, dev);
  839. if (ret) {
  840. dev_err(&dev->pdev->dev, "SMI IRQ allocation failed\n");
  841. goto err_irq;
  842. }
  843. mutex_init(&dev->lock);
  844. init_waitqueue_head(&dev->cmd_complete);
  845. spear_smi_hw_init(dev);
  846. platform_set_drvdata(pdev, dev);
  847. /* loop for each serial nor-flash which is connected to smi */
  848. for (i = 0; i < dev->num_flashes; i++) {
  849. ret = spear_smi_setup_banks(pdev, i, pdata->np[i]);
  850. if (ret) {
  851. dev_err(&dev->pdev->dev, "bank setup failed\n");
  852. goto err_bank_setup;
  853. }
  854. }
  855. return 0;
  856. err_bank_setup:
  857. free_irq(irq, dev);
  858. platform_set_drvdata(pdev, NULL);
  859. err_irq:
  860. clk_disable_unprepare(dev->clk);
  861. err_clk_prepare_enable:
  862. clk_put(dev->clk);
  863. err_clk:
  864. iounmap(dev->io_base);
  865. err_ioremap:
  866. release_mem_region(smi_base->start, resource_size(smi_base));
  867. err_mem:
  868. kfree(dev);
  869. err:
  870. return ret;
  871. }
  872. /**
  873. * spear_smi_remove - Exit routine
  874. * @pdev: platform device structure
  875. *
  876. * free all allocations and delete the partitions.
  877. */
  878. static int __devexit spear_smi_remove(struct platform_device *pdev)
  879. {
  880. struct spear_smi *dev;
  881. struct spear_smi_plat_data *pdata;
  882. struct spear_snor_flash *flash;
  883. struct resource *smi_base;
  884. int ret;
  885. int i, irq;
  886. dev = platform_get_drvdata(pdev);
  887. if (!dev) {
  888. dev_err(&pdev->dev, "dev is null\n");
  889. return -ENODEV;
  890. }
  891. pdata = dev_get_platdata(&pdev->dev);
  892. /* clean up for all nor flash */
  893. for (i = 0; i < dev->num_flashes; i++) {
  894. flash = dev->flash[i];
  895. if (!flash)
  896. continue;
  897. /* clean up mtd stuff */
  898. ret = mtd_device_unregister(&flash->mtd);
  899. if (ret)
  900. dev_err(&pdev->dev, "error removing mtd\n");
  901. iounmap(flash->base_addr);
  902. kfree(flash);
  903. }
  904. irq = platform_get_irq(pdev, 0);
  905. free_irq(irq, dev);
  906. clk_disable_unprepare(dev->clk);
  907. clk_put(dev->clk);
  908. iounmap(dev->io_base);
  909. kfree(dev);
  910. smi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  911. release_mem_region(smi_base->start, resource_size(smi_base));
  912. platform_set_drvdata(pdev, NULL);
  913. return 0;
  914. }
  915. #ifdef CONFIG_PM
  916. static int spear_smi_suspend(struct device *dev)
  917. {
  918. struct spear_smi *sdev = dev_get_drvdata(dev);
  919. if (sdev && sdev->clk)
  920. clk_disable_unprepare(sdev->clk);
  921. return 0;
  922. }
  923. static int spear_smi_resume(struct device *dev)
  924. {
  925. struct spear_smi *sdev = dev_get_drvdata(dev);
  926. int ret = -EPERM;
  927. if (sdev && sdev->clk)
  928. ret = clk_prepare_enable(sdev->clk);
  929. if (!ret)
  930. spear_smi_hw_init(sdev);
  931. return ret;
  932. }
  933. static SIMPLE_DEV_PM_OPS(spear_smi_pm_ops, spear_smi_suspend, spear_smi_resume);
  934. #endif
  935. #ifdef CONFIG_OF
  936. static const struct of_device_id spear_smi_id_table[] = {
  937. { .compatible = "st,spear600-smi" },
  938. {}
  939. };
  940. MODULE_DEVICE_TABLE(of, spear_smi_id_table);
  941. #endif
  942. static struct platform_driver spear_smi_driver = {
  943. .driver = {
  944. .name = "smi",
  945. .bus = &platform_bus_type,
  946. .owner = THIS_MODULE,
  947. .of_match_table = of_match_ptr(spear_smi_id_table),
  948. #ifdef CONFIG_PM
  949. .pm = &spear_smi_pm_ops,
  950. #endif
  951. },
  952. .probe = spear_smi_probe,
  953. .remove = __devexit_p(spear_smi_remove),
  954. };
  955. static int spear_smi_init(void)
  956. {
  957. return platform_driver_register(&spear_smi_driver);
  958. }
  959. module_init(spear_smi_init);
  960. static void spear_smi_exit(void)
  961. {
  962. platform_driver_unregister(&spear_smi_driver);
  963. }
  964. module_exit(spear_smi_exit);
  965. MODULE_LICENSE("GPL");
  966. MODULE_AUTHOR("Ashish Priyadarshi, Shiraz Hashim <shiraz.hashim@st.com>");
  967. MODULE_DESCRIPTION("MTD SMI driver for serial nor flash chips");