floppy.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. #include <rtthread.h>
  2. #include <rthw.h>
  3. #include <bsp.h>
  4. typedef rt_uint8_t u8;
  5. typedef rt_uint16_t u16;
  6. typedef rt_uint32_t u32;
  7. typedef rt_int8_t s8;
  8. typedef rt_int16_t s16;
  9. typedef rt_int32_t s32;
  10. #define OUTB(v,p) outb(p,v)
  11. #include "floppy.h"
  12. #include "dma.h"
  13. #define NULL RT_NULL
  14. #define SECTOR_SIZE 512
  15. #define panic(str,...) do { rt_kprintf("panic::" str,##__VA_ARGS__); while(1); } while(0)
  16. #define _local_irq_save(level) level = rt_hw_interrupt_disable()
  17. #define _local_irq_restore(level) rt_hw_interrupt_enable(level)
  18. static u8 floppy_buffer[512]; /* 软盘高速缓冲区地址指针 */
  19. #define MAX_REPLIES 7
  20. static u8 floppy_reply_buffer[MAX_REPLIES]; /* 软驱回应缓冲区 */
  21. #define ST0 (floppy_reply_buffer[0]) /* 软驱回应0号字节 */
  22. #define ST1 (floppy_reply_buffer[1]) /* 软驱回应1号字节 */
  23. #define ST2 (floppy_reply_buffer[2]) /* 软驱回应2号字节 */
  24. #define ST3 (floppy_reply_buffer[3]) /* 软驱回应3号字节 */
  25. static char *floppy_inc_name; /* 软驱型号名 */
  26. static char *floppy_type;
  27. static u32 floppy_motor=0; /* 软驱马达状态字节 */
  28. static u32 floppy_size =0;
  29. /**********************功能函数***************************/
  30. static void floppy_result(void); /* 获得软驱响应状态 */
  31. static u32 floppy_sendbyte(u32); /* 向软驱控制寄存器发送一个控制字节 */
  32. static u32 floppy_getbyte(void); /* 从软驱数据寄存器得到一个数据字节 */
  33. static u32 floppy_get_info(void); /* 得到软驱信息 */
  34. static void floppy_motorOn(void); /* 打开软驱马达 */
  35. static void floppy_motorOff(void); /* 关闭软驱马达 */
  36. static void floppy_setmode(void); /* 软驱模式设置 */
  37. static void block_to_hts(u32, u32*, u32*, u32*); /* 逻辑块转为磁盘头、磁道号和扇区号 */
  38. static void floppy_setupDMA(void); /* 设置软驱DMA通道 */
  39. static void floppy_read_cmd(u32 blk); /* 从软盘上读取指定的逻辑块到缓冲区 */
  40. void floppy_result(void)
  41. {
  42. u8 stat, i,count;
  43. i=0;
  44. for(count=0; count<0xFF; count++)
  45. {
  46. stat = inb( FD_STATUS ) & (STATUS_READY|STATUS_DIR|STATUS_BUSY); //读取状态寄存器
  47. if (stat == STATUS_READY)
  48. return;
  49. if (stat == (STATUS_READY|STATUS_DIR|STATUS_BUSY))
  50. {
  51. if(i>7) break;
  52. floppy_reply_buffer[i++]=inb_p(FD_DATA);
  53. }
  54. }
  55. panic("Get floppy status times out !\n");
  56. }
  57. u32 floppy_sendbyte( u32 value )
  58. {
  59. u8 stat, i;
  60. for ( i = 0; i < 128; i++ ) {
  61. stat = inb( FD_STATUS ) & (STATUS_READY|STATUS_DIR); //读取状态寄存器
  62. if ( stat == STATUS_READY )
  63. {
  64. OUTB( value ,FD_DATA); //将参数写入数据寄存器
  65. return 1;
  66. }
  67. io_delay(); // 作一些延迟
  68. }
  69. return 0;
  70. }
  71. u32 floppy_getbyte(void)
  72. {
  73. u8 stat, i;
  74. for ( i = 0; i < 128; i++ ) {
  75. stat = inb( FD_STATUS ) & (STATUS_READY|STATUS_DIR|STATUS_BUSY); //读取状态寄存器
  76. if (stat == STATUS_READY)
  77. return -1;
  78. if ( stat == 0xD0 )
  79. return inb(FD_DATA);
  80. io_delay();
  81. }
  82. return 0;
  83. }
  84. u32 floppy_get_info(void)
  85. {
  86. u32 i;
  87. u8 CmType, FdType;
  88. floppy_sendbyte(0x10);
  89. i = floppy_getbyte();
  90. switch (i)
  91. {
  92. case 0x80: floppy_inc_name = "NEC765A controller"; break;
  93. case 0x90: floppy_inc_name = "NEC765B controller"; break;
  94. default: floppy_inc_name = "Enhanced controller"; break;
  95. }
  96. CmType = readcmos(0x10); //read floppy type from cmos
  97. FdType = (CmType>>4) & 0x07;
  98. if ( FdType == 0 )
  99. panic("Floppy driver not found!");
  100. switch( FdType )
  101. {
  102. case 0x02: // 1.2MB
  103. floppy_type = "1.2MB";
  104. floppy_size = 2458*512;
  105. break;
  106. case 0x04: // 1.44MB 标准软盘
  107. floppy_type = "1.44MB";
  108. floppy_size = 2880*512;
  109. break;
  110. case 0x05: // 2.88MB
  111. floppy_type = "2.88MB";
  112. floppy_size = 2*2880*512;
  113. break;
  114. }
  115. return 1;
  116. }
  117. void floppy_motorOn( void )
  118. {
  119. u32 eflags;
  120. if (!floppy_motor)
  121. {
  122. _local_irq_save(eflags);
  123. OUTB(28,FD_DOR);
  124. floppy_motor = 1;
  125. _local_irq_restore(eflags);
  126. }
  127. return;
  128. }
  129. void floppy_motorOff( void )
  130. {
  131. u32 eflags;
  132. if (floppy_motor)
  133. {
  134. _local_irq_save(eflags);
  135. OUTB(12,FD_DOR);
  136. floppy_motor = 0;
  137. _local_irq_restore(eflags);
  138. }
  139. return;
  140. }
  141. void floppy_setmode(void)
  142. {
  143. floppy_sendbyte (FD_SPECIFY);
  144. floppy_sendbyte (0xcf);
  145. floppy_sendbyte (0x06);
  146. OUTB (0,FD_DCR);
  147. }
  148. void block_to_hts(u32 block, u32 *head, u32 *track, u32 *sector )
  149. {
  150. *head = ( block % ( 18 * 2 ) ) /18;
  151. *track = block / ( 18 * 2 );
  152. *sector = block % 18 + 1;
  153. }
  154. void floppy_setupDMA(void)
  155. {
  156. u32 eflags;
  157. _local_irq_save(eflags);
  158. DisableDma(2);
  159. ClearDmaFF(2);
  160. SetDmaMode(2,DMA_MODE_READ);
  161. SetDmaAddr(2,(unsigned long)floppy_buffer);
  162. SetDmaCount(2,512);
  163. EnableDma(2);
  164. _local_irq_restore(eflags);
  165. }
  166. void floppy_read_cmd(u32 blk)
  167. {
  168. u32 head;
  169. u32 track;
  170. u32 sector;
  171. block_to_hts(blk,&head,&track,&sector);
  172. floppy_motorOn();
  173. io_delay();
  174. floppy_setupDMA();
  175. io_delay();
  176. floppy_setmode();
  177. io_delay();
  178. floppy_sendbyte (FD_READ); //send read command
  179. floppy_sendbyte (head*4 + 0);
  180. floppy_sendbyte (track); /* Cylinder */
  181. floppy_sendbyte (head); /* Head */
  182. floppy_sendbyte (sector); /* Sector */
  183. floppy_sendbyte (2); /* 0=128, 1=256, 2=512, 3=1024, ... */
  184. floppy_sendbyte (18);
  185. //floppy_sendbyte (sector+secs-1); /* Last sector in track:here are sectors count */
  186. floppy_sendbyte (0x1B);
  187. floppy_sendbyte (0xff);
  188. return;
  189. }
  190. static struct rt_device devF;
  191. static struct rt_mutex lock;
  192. static struct rt_semaphore sem;
  193. /* RT-Thread device interface */
  194. static rt_err_t rt_floppy_init_internal(rt_device_t dev)
  195. {
  196. return RT_EOK;
  197. }
  198. static rt_err_t rt_floppy_open(rt_device_t dev, rt_uint16_t oflag)
  199. {
  200. return RT_EOK;
  201. }
  202. static rt_err_t rt_floppy_close(rt_device_t dev)
  203. {
  204. return RT_EOK;
  205. }
  206. /* position: block page address, not bytes address
  207. * buffer:
  208. * size : how many blocks
  209. */
  210. static rt_size_t rt_floppy_read(rt_device_t device, rt_off_t position, void *buffer, rt_size_t size)
  211. {
  212. rt_size_t doSize = size;
  213. rt_mutex_take(&lock, RT_WAITING_FOREVER);
  214. while(size>0)
  215. {
  216. floppy_read_cmd(position);
  217. rt_sem_take(&sem, RT_WAITING_FOREVER); /* waiting isr sem forever */
  218. floppy_result();
  219. io_delay();
  220. if(ST1 != 0 || ST2 != 0)
  221. {
  222. panic("ST0 %d ST1 %d ST2 %d\n",ST0,ST1,ST2);
  223. }
  224. rt_memcpy(buffer, floppy_buffer, 512);
  225. floppy_motorOff();
  226. io_delay();
  227. position += 1;
  228. size -= 1;
  229. }
  230. rt_mutex_release(&lock);
  231. return doSize;
  232. }
  233. /* position: block page address, not bytes address
  234. * buffer:
  235. * size : how many blocks
  236. */
  237. static rt_size_t rt_floppy_write(rt_device_t device, rt_off_t position, const void *buffer, rt_size_t size)
  238. {
  239. rt_mutex_take(&lock, RT_WAITING_FOREVER);
  240. panic("FIXME:I don't know how!\n");
  241. rt_mutex_release(&lock);
  242. return size;
  243. }
  244. static rt_err_t rt_floppy_control(rt_device_t dev, int cmd, void *args)
  245. {
  246. RT_ASSERT(dev != RT_NULL);
  247. if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
  248. {
  249. struct rt_device_blk_geometry *geometry;
  250. geometry = (struct rt_device_blk_geometry *)args;
  251. if (geometry == RT_NULL) return -RT_ERROR;
  252. geometry->bytes_per_sector = SECTOR_SIZE;
  253. geometry->block_size = SECTOR_SIZE;
  254. geometry->sector_count = floppy_size / SECTOR_SIZE;
  255. }
  256. return RT_EOK;
  257. }
  258. static void rt_floppy_isr(int vector, void* param)
  259. {
  260. (void)vector;
  261. (void)param;
  262. rt_sem_release(&sem);
  263. }
  264. void rt_floppy_init(void)
  265. {
  266. struct rt_device *device;
  267. rt_mutex_init(&lock,"fdlock", RT_IPC_FLAG_FIFO);
  268. rt_sem_init(&sem, "fdsem", 0, RT_IPC_FLAG_FIFO);
  269. rt_hw_interrupt_install(FLOPPY_IRQ, rt_floppy_isr, RT_NULL, "floppy");
  270. rt_hw_interrupt_umask(FLOPPY_IRQ);
  271. floppy_get_info();
  272. rt_kprintf("Floppy Inc : %s Floppy Type : %s\n",floppy_inc_name,floppy_type);
  273. device = &(devF);
  274. device->type = RT_Device_Class_Block;
  275. device->init = rt_floppy_init_internal;
  276. device->open = rt_floppy_open;
  277. device->close = rt_floppy_close;
  278. device->read = rt_floppy_read;
  279. device->write = rt_floppy_write;
  280. device->control = rt_floppy_control;
  281. device->user_data = NULL;
  282. rt_device_register(device, "floppy",
  283. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
  284. }