1 | /* |
---|
2 | * s3c2400 smc disk block device implementation |
---|
3 | * |
---|
4 | * Squidge's SMC Low-level access routines. |
---|
5 | * Inspired and derived from routines provided by Samsung Electronics |
---|
6 | * M/M R&D Center & FireFly. |
---|
7 | */ |
---|
8 | |
---|
9 | #include <rtems.h> |
---|
10 | #include <rtems/libio.h> |
---|
11 | #include <errno.h> |
---|
12 | #include <stdlib.h> |
---|
13 | #include <stdio.h> |
---|
14 | #include <string.h> |
---|
15 | #include <inttypes.h> |
---|
16 | |
---|
17 | #include "rtems/blkdev.h" |
---|
18 | #include "rtems/diskdevs.h" |
---|
19 | #include "smc.h" |
---|
20 | #include <rtems/bspIo.h> |
---|
21 | #include <s3c24xx.h> |
---|
22 | |
---|
23 | #define SMC_DEVICE_NAME "/dev/smc" |
---|
24 | #define SMC_SAMSUNG_ID 0xEC |
---|
25 | #define SMC_TOSHIBA_ID 0x98 |
---|
26 | |
---|
27 | #define SMC_16MB 0x73 |
---|
28 | #define SMC_32MB 0x75 |
---|
29 | #define SMC_64MB 0x76 |
---|
30 | #define SMC_128MB 0x79 |
---|
31 | |
---|
32 | #define LBA_UNUSED 0x80000000 |
---|
33 | #define LBA_RESERVED 0x80000001 |
---|
34 | |
---|
35 | #define BLOCK_UNUSED 0x80000000 |
---|
36 | #define BLOCK_RESERVED 0x80000001 |
---|
37 | |
---|
38 | /* SmartMedia Command */ |
---|
39 | #define SEQ_DATA_INPUT_CMD 0x80 |
---|
40 | #define READ1_CMD 0x00 |
---|
41 | #define READ1_1_CMD 0x01 |
---|
42 | #define READ2_CMD 0x50 |
---|
43 | #define READ_ID_CMD 0x90 |
---|
44 | #define RESET_CMD 0xFF |
---|
45 | #define PAGE_PROGRAM_CMD 0x10 |
---|
46 | #define BLOCK_ERASE_CMD 0x60 |
---|
47 | #define BLOCK_ERASE_CFM_CMD 0xD0 |
---|
48 | #define READ_STATUS_CMD 0x70 |
---|
49 | #define RESET_PTR_CMD 0x00 |
---|
50 | |
---|
51 | |
---|
52 | /* Internal SMC disk descriptor */ |
---|
53 | struct SMC_INFO { |
---|
54 | uint8_t id[3]; |
---|
55 | uint32_t bytes_per_page; |
---|
56 | uint32_t pages_per_block; |
---|
57 | uint32_t blocks; |
---|
58 | uint32_t mb; |
---|
59 | }; |
---|
60 | |
---|
61 | /* Ths S3c2410 uses a different register map */ |
---|
62 | #ifdef CPU_S3C2410 |
---|
63 | #define rPBDAT rGPBDAT |
---|
64 | #define rPBCON rGPBCON |
---|
65 | #define rPDDAT rGPDDAT |
---|
66 | #define rPEDAT rGPEDAT |
---|
67 | #endif |
---|
68 | |
---|
69 | |
---|
70 | static struct SMC_INFO smc_info; |
---|
71 | |
---|
72 | uint32_t smc_l2p[0x2000]; |
---|
73 | uint32_t smc_p2l[0x2000]; |
---|
74 | |
---|
75 | #define sm_busy() while (!(rPDDAT & 0x200)) |
---|
76 | #define sm_chip_en() rPDDAT &= (~0x80) |
---|
77 | #define sm_chip_dis() rPDDAT |= 0x80 |
---|
78 | #define sm_cle_en() rPEDAT |= 0x20 |
---|
79 | #define sm_cle_dis() rPEDAT &= (~0x20) |
---|
80 | #define sm_ale_en() rPEDAT |= 0x10 |
---|
81 | #define sm_ale_dis() rPEDAT &= (~0x10) |
---|
82 | #define sm_wp_en() rPDDAT &= (~0x40) |
---|
83 | #define sm_wp_dis() rPDDAT |= 0x40 |
---|
84 | #define sm_read_en() rPBCON &= 0xFFFF0000 |
---|
85 | #define sm_read_dis() rPBCON = (rPBCON & 0xFFFF0000) | 0x5555 |
---|
86 | #define sm_write_en() sm_read_dis() |
---|
87 | #define sm_write_dis() sm_read_en() |
---|
88 | |
---|
89 | static void sm_write( uint8_t data) |
---|
90 | { |
---|
91 | rPBDAT = (rPBDAT & 0xFF00) | data; |
---|
92 | rPEDAT &= (~0x08); |
---|
93 | rPEDAT |= 0x08; |
---|
94 | } |
---|
95 | |
---|
96 | static uint8_t sm_read(void) |
---|
97 | { |
---|
98 | uint8_t data; |
---|
99 | |
---|
100 | rPDDAT &= (~0x100); |
---|
101 | data = rPBDAT & 0xFF; |
---|
102 | rPDDAT |= 0x100; |
---|
103 | return data; |
---|
104 | } |
---|
105 | |
---|
106 | static void smc_read_id( uint8_t* buf, uint32_t length) |
---|
107 | { |
---|
108 | uint32_t i; |
---|
109 | |
---|
110 | sm_chip_en(); |
---|
111 | |
---|
112 | sm_cle_en(); |
---|
113 | sm_write_en(); |
---|
114 | sm_write(READ_ID_CMD); |
---|
115 | sm_write_dis(); |
---|
116 | sm_cle_dis(); |
---|
117 | |
---|
118 | sm_ale_en(); |
---|
119 | sm_write_en(); |
---|
120 | sm_write( 0); |
---|
121 | sm_write_dis(); |
---|
122 | sm_ale_dis(); |
---|
123 | |
---|
124 | sm_read_en(); |
---|
125 | for (i=0;i<length;i++) *(buf+i) = sm_read(); |
---|
126 | sm_read_dis(); |
---|
127 | |
---|
128 | sm_chip_dis(); |
---|
129 | } |
---|
130 | |
---|
131 | /* read an entire logical page of 512 bytes.*/ |
---|
132 | static uint8_t smc_read_page (uint32_t lpage, uint8_t* buf) |
---|
133 | { |
---|
134 | uint32_t block, page, i; |
---|
135 | |
---|
136 | /* convert logical block to physical block |
---|
137 | and then convert into page suitable for read1 command... |
---|
138 | */ |
---|
139 | block = lpage >> 5; |
---|
140 | if (smc_l2p[block] < LBA_UNUSED) { |
---|
141 | page = smc_l2p[block] << 5; |
---|
142 | page += (lpage & 0x1F); |
---|
143 | } |
---|
144 | else |
---|
145 | return 0; |
---|
146 | |
---|
147 | sm_chip_en(); |
---|
148 | |
---|
149 | sm_cle_en(); |
---|
150 | sm_write_en(); |
---|
151 | sm_write(READ1_CMD); |
---|
152 | sm_write_dis(); |
---|
153 | sm_cle_dis(); |
---|
154 | |
---|
155 | sm_ale_en(); |
---|
156 | sm_write_en(); |
---|
157 | sm_write( 0x00); |
---|
158 | sm_write( (uint8_t)(page >> 0)); |
---|
159 | sm_write( (uint8_t)(page >> 8)); |
---|
160 | if (smc_info.mb >= 64) |
---|
161 | sm_write( (uint8_t)(page >> 16)); |
---|
162 | sm_write_dis(); |
---|
163 | sm_ale_dis(); |
---|
164 | |
---|
165 | sm_busy(); |
---|
166 | |
---|
167 | sm_read_en(); |
---|
168 | for (i = 0; i < 512; i++) { |
---|
169 | *buf = sm_read(); |
---|
170 | buf++; |
---|
171 | } |
---|
172 | sm_read_dis(); |
---|
173 | sm_chip_dis(); |
---|
174 | |
---|
175 | sm_busy(); |
---|
176 | return 1; |
---|
177 | } |
---|
178 | |
---|
179 | static void smc_read_spare( uint32_t page, uint8_t* buf, uint8_t length) |
---|
180 | { |
---|
181 | uint32_t i; |
---|
182 | |
---|
183 | sm_chip_en(); |
---|
184 | |
---|
185 | sm_cle_en(); |
---|
186 | sm_read_dis(); |
---|
187 | sm_write(READ2_CMD); |
---|
188 | sm_read_en(); |
---|
189 | sm_cle_dis(); |
---|
190 | |
---|
191 | sm_ale_en(); |
---|
192 | sm_read_dis(); |
---|
193 | sm_write( 0x00); |
---|
194 | sm_write( (uint8_t)(page >> 0)); |
---|
195 | sm_write( (uint8_t)(page >> 8)); |
---|
196 | if (smc_info.mb >= 64) |
---|
197 | sm_write( (uint8_t)(page >> 16)); |
---|
198 | sm_read_en(); |
---|
199 | sm_ale_dis(); |
---|
200 | |
---|
201 | sm_busy(); |
---|
202 | |
---|
203 | sm_read_en(); |
---|
204 | for (i=0;i<length;i++) |
---|
205 | *(buf+i) = sm_read(); |
---|
206 | sm_read_dis(); |
---|
207 | |
---|
208 | sm_chip_dis(); |
---|
209 | |
---|
210 | } |
---|
211 | |
---|
212 | static void smc_make_l2p(void) |
---|
213 | { |
---|
214 | uint32_t pblock, i, j, lblock, zone, count; |
---|
215 | uint8_t data[512]; |
---|
216 | |
---|
217 | for (i=0;i<0x2000;i++) { |
---|
218 | smc_l2p[i] = LBA_RESERVED; |
---|
219 | smc_p2l[i] = BLOCK_RESERVED; |
---|
220 | } |
---|
221 | |
---|
222 | for (pblock=0;pblock<smc_info.blocks;pblock++) { |
---|
223 | /* read physical block - first page */ |
---|
224 | smc_read_spare( pblock*smc_info.pages_per_block, (uint8_t*)&data, 16); |
---|
225 | |
---|
226 | zone = pblock >> 10; /* divide by 1024 to get zone */ |
---|
227 | if ((data[5] == 0xFF) && ((data[6]&0xF8) == 0x10)) { |
---|
228 | lblock = ((((data[6]<<8)|(data[7]<<0)) >> 1) & 0x03FF) + (zone * 1000); |
---|
229 | smc_l2p[lblock] = pblock; |
---|
230 | smc_p2l[pblock] = lblock; |
---|
231 | } else { |
---|
232 | count = 0; |
---|
233 | for (j=0;j<16;j++) { |
---|
234 | if (data[j] == 0xFF) count++; |
---|
235 | } |
---|
236 | if (count == 16) { |
---|
237 | smc_p2l[pblock] = BLOCK_UNUSED; |
---|
238 | } else { |
---|
239 | smc_p2l[pblock] = BLOCK_RESERVED; |
---|
240 | } |
---|
241 | } |
---|
242 | } |
---|
243 | } |
---|
244 | |
---|
245 | |
---|
246 | static void smc_detect( uint8_t id1, uint8_t id2, uint8_t id3) |
---|
247 | { |
---|
248 | smc_info.id[0] = id1; |
---|
249 | smc_info.id[1] = id2; |
---|
250 | smc_info.id[2] = id3; |
---|
251 | smc_info.mb = 0; |
---|
252 | smc_info.bytes_per_page = 0; |
---|
253 | smc_info.pages_per_block = 0; |
---|
254 | smc_info.blocks = 0; |
---|
255 | |
---|
256 | switch (id1) { |
---|
257 | case SMC_SAMSUNG_ID: |
---|
258 | case SMC_TOSHIBA_ID: { |
---|
259 | switch (id2) { |
---|
260 | case SMC_16MB : smc_info.mb = 16; break; |
---|
261 | case SMC_32MB : smc_info.mb = 32; break; |
---|
262 | case SMC_64MB : smc_info.mb = 64; break; |
---|
263 | case SMC_128MB : smc_info.mb = 128; break; |
---|
264 | } |
---|
265 | break; |
---|
266 | } |
---|
267 | } |
---|
268 | |
---|
269 | switch (smc_info.mb) { |
---|
270 | case 16 : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x0400; break; |
---|
271 | case 32 : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x0800; break; |
---|
272 | case 64 : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x1000; break; |
---|
273 | case 128 : smc_info.bytes_per_page = 512; smc_info.pages_per_block = 32; smc_info.blocks = 0x2000; break; |
---|
274 | } |
---|
275 | } |
---|
276 | |
---|
277 | static void smc_init( void) |
---|
278 | { |
---|
279 | unsigned char buf[32]; |
---|
280 | int i; |
---|
281 | |
---|
282 | /* reset smc */ |
---|
283 | sm_chip_en(); |
---|
284 | sm_cle_en(); |
---|
285 | sm_write_en(); |
---|
286 | sm_write(0xFF); |
---|
287 | sm_write_dis(); |
---|
288 | sm_cle_dis(); |
---|
289 | for(i=0;i<10;i++); |
---|
290 | sm_busy(); |
---|
291 | sm_chip_dis(); |
---|
292 | |
---|
293 | smc_read_id (buf, 4); |
---|
294 | smc_detect (buf[0], buf[1], buf[2]); |
---|
295 | printk ("SMC: [%02X-%02X-%02X-%02X]\n", buf[0], buf[1], buf[2], buf[3]); |
---|
296 | printk ("SMC size: %" PRIu32 "MB detected\n",smc_info.mb); |
---|
297 | smc_make_l2p(); |
---|
298 | } |
---|
299 | |
---|
300 | /* smc_write -- |
---|
301 | * write stub |
---|
302 | */ |
---|
303 | static int smc_write(rtems_blkdev_request *req) |
---|
304 | { |
---|
305 | rtems_blkdev_request_done(req, RTEMS_SUCCESSFUL); |
---|
306 | return 0; |
---|
307 | } |
---|
308 | |
---|
309 | |
---|
310 | /* smc_read -- |
---|
311 | * PARAMETERS: |
---|
312 | * req - pointer to the READ block device request info |
---|
313 | * |
---|
314 | * RETURNS: |
---|
315 | * ioctl return value |
---|
316 | */ |
---|
317 | static int |
---|
318 | smc_read(rtems_blkdev_request *req) |
---|
319 | { |
---|
320 | uint32_t i; |
---|
321 | rtems_blkdev_sg_buffer *sg; |
---|
322 | uint32_t remains; |
---|
323 | |
---|
324 | remains = smc_info.bytes_per_page * req->bufnum; |
---|
325 | sg = req->bufs; |
---|
326 | for (i = 0; (remains > 0) && (i < req->bufnum); i++, sg++) |
---|
327 | { |
---|
328 | int count = sg->length; |
---|
329 | if (count > remains) |
---|
330 | count = remains; |
---|
331 | smc_read_page(sg->block,sg->buffer); |
---|
332 | remains -= count; |
---|
333 | } |
---|
334 | rtems_blkdev_request_done(req, RTEMS_SUCCESSFUL); |
---|
335 | return 0; |
---|
336 | } |
---|
337 | |
---|
338 | /* smc_ioctl -- |
---|
339 | * IOCTL handler for SMC device. |
---|
340 | * |
---|
341 | * PARAMETERS: |
---|
342 | * dev - device number (major, minor number) |
---|
343 | * req - IOCTL request code |
---|
344 | * argp - IOCTL argument |
---|
345 | * |
---|
346 | * RETURNS: |
---|
347 | * IOCTL return value |
---|
348 | */ |
---|
349 | static int |
---|
350 | smc_ioctl(rtems_disk_device *dd, uint32_t req, void *argp) |
---|
351 | { |
---|
352 | switch (req) |
---|
353 | { |
---|
354 | case RTEMS_BLKIO_REQUEST: |
---|
355 | { |
---|
356 | rtems_blkdev_request *r = argp; |
---|
357 | switch (r->req) |
---|
358 | { |
---|
359 | case RTEMS_BLKDEV_REQ_READ: |
---|
360 | return smc_read(r); |
---|
361 | case RTEMS_BLKDEV_REQ_WRITE: |
---|
362 | return smc_write(r); |
---|
363 | default: |
---|
364 | errno = EINVAL; |
---|
365 | return -1; |
---|
366 | } |
---|
367 | break; |
---|
368 | } |
---|
369 | |
---|
370 | default: |
---|
371 | errno = EINVAL; |
---|
372 | return -1; |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | /* smc_initialize -- |
---|
377 | * RAM disk device driver initialization. Run through RAM disk |
---|
378 | * configuration information and configure appropriate RAM disks. |
---|
379 | * |
---|
380 | * PARAMETERS: |
---|
381 | * major - RAM disk major device number |
---|
382 | * minor - minor device number, not applicable |
---|
383 | * arg - initialization argument, not applicable |
---|
384 | * |
---|
385 | * RETURNS: |
---|
386 | * none |
---|
387 | */ |
---|
388 | rtems_device_driver |
---|
389 | smc_initialize( |
---|
390 | rtems_device_major_number major, |
---|
391 | rtems_device_minor_number minor, |
---|
392 | void *arg) |
---|
393 | { |
---|
394 | rtems_status_code rc; |
---|
395 | dev_t dev; |
---|
396 | uint32_t block_num; |
---|
397 | |
---|
398 | rc = rtems_disk_io_initialize(); |
---|
399 | if (rc != RTEMS_SUCCESSFUL) |
---|
400 | return rc; |
---|
401 | |
---|
402 | smc_init(); |
---|
403 | block_num = smc_info.blocks << 5; |
---|
404 | |
---|
405 | dev = rtems_filesystem_make_dev_t(major, 0); |
---|
406 | rc = rtems_disk_create_phys(dev, 512, block_num, |
---|
407 | smc_ioctl, NULL, SMC_DEVICE_NAME); |
---|
408 | |
---|
409 | return RTEMS_SUCCESSFUL; |
---|
410 | } |
---|