spear_smi.c 29 KB

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