18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Driver for SWIM (Sander Woz Integrated Machine) floppy controller 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (C) 2004,2008 Laurent Vivier <Laurent@lvivier.info> 68c2ecf20Sopenharmony_ci * 78c2ecf20Sopenharmony_ci * based on Alastair Bridgewater SWIM analysis, 2001 88c2ecf20Sopenharmony_ci * based on SWIM3 driver (c) Paul Mackerras, 1996 98c2ecf20Sopenharmony_ci * based on netBSD IWM driver (c) 1997, 1998 Hauke Fath. 108c2ecf20Sopenharmony_ci * 118c2ecf20Sopenharmony_ci * 2004-08-21 (lv) - Initial implementation 128c2ecf20Sopenharmony_ci * 2008-10-30 (lv) - Port to 2.6 138c2ecf20Sopenharmony_ci */ 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci#include <linux/module.h> 168c2ecf20Sopenharmony_ci#include <linux/fd.h> 178c2ecf20Sopenharmony_ci#include <linux/slab.h> 188c2ecf20Sopenharmony_ci#include <linux/blk-mq.h> 198c2ecf20Sopenharmony_ci#include <linux/mutex.h> 208c2ecf20Sopenharmony_ci#include <linux/hdreg.h> 218c2ecf20Sopenharmony_ci#include <linux/kernel.h> 228c2ecf20Sopenharmony_ci#include <linux/delay.h> 238c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#include <asm/mac_via.h> 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define CARDNAME "swim" 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistruct sector_header { 308c2ecf20Sopenharmony_ci unsigned char side; 318c2ecf20Sopenharmony_ci unsigned char track; 328c2ecf20Sopenharmony_ci unsigned char sector; 338c2ecf20Sopenharmony_ci unsigned char size; 348c2ecf20Sopenharmony_ci unsigned char crc0; 358c2ecf20Sopenharmony_ci unsigned char crc1; 368c2ecf20Sopenharmony_ci} __attribute__((packed)); 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_ci#define DRIVER_VERSION "Version 0.2 (2008-10-30)" 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci#define REG(x) unsigned char x, x ## _pad[0x200 - 1]; 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_cistruct swim { 438c2ecf20Sopenharmony_ci REG(write_data) 448c2ecf20Sopenharmony_ci REG(write_mark) 458c2ecf20Sopenharmony_ci REG(write_CRC) 468c2ecf20Sopenharmony_ci REG(write_parameter) 478c2ecf20Sopenharmony_ci REG(write_phase) 488c2ecf20Sopenharmony_ci REG(write_setup) 498c2ecf20Sopenharmony_ci REG(write_mode0) 508c2ecf20Sopenharmony_ci REG(write_mode1) 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci REG(read_data) 538c2ecf20Sopenharmony_ci REG(read_mark) 548c2ecf20Sopenharmony_ci REG(read_error) 558c2ecf20Sopenharmony_ci REG(read_parameter) 568c2ecf20Sopenharmony_ci REG(read_phase) 578c2ecf20Sopenharmony_ci REG(read_setup) 588c2ecf20Sopenharmony_ci REG(read_status) 598c2ecf20Sopenharmony_ci REG(read_handshake) 608c2ecf20Sopenharmony_ci} __attribute__((packed)); 618c2ecf20Sopenharmony_ci 628c2ecf20Sopenharmony_ci#define swim_write(base, reg, v) out_8(&(base)->write_##reg, (v)) 638c2ecf20Sopenharmony_ci#define swim_read(base, reg) in_8(&(base)->read_##reg) 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci/* IWM registers */ 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_cistruct iwm { 688c2ecf20Sopenharmony_ci REG(ph0L) 698c2ecf20Sopenharmony_ci REG(ph0H) 708c2ecf20Sopenharmony_ci REG(ph1L) 718c2ecf20Sopenharmony_ci REG(ph1H) 728c2ecf20Sopenharmony_ci REG(ph2L) 738c2ecf20Sopenharmony_ci REG(ph2H) 748c2ecf20Sopenharmony_ci REG(ph3L) 758c2ecf20Sopenharmony_ci REG(ph3H) 768c2ecf20Sopenharmony_ci REG(mtrOff) 778c2ecf20Sopenharmony_ci REG(mtrOn) 788c2ecf20Sopenharmony_ci REG(intDrive) 798c2ecf20Sopenharmony_ci REG(extDrive) 808c2ecf20Sopenharmony_ci REG(q6L) 818c2ecf20Sopenharmony_ci REG(q6H) 828c2ecf20Sopenharmony_ci REG(q7L) 838c2ecf20Sopenharmony_ci REG(q7H) 848c2ecf20Sopenharmony_ci} __attribute__((packed)); 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci#define iwm_write(base, reg, v) out_8(&(base)->reg, (v)) 878c2ecf20Sopenharmony_ci#define iwm_read(base, reg) in_8(&(base)->reg) 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci/* bits in phase register */ 908c2ecf20Sopenharmony_ci 918c2ecf20Sopenharmony_ci#define SEEK_POSITIVE 0x070 928c2ecf20Sopenharmony_ci#define SEEK_NEGATIVE 0x074 938c2ecf20Sopenharmony_ci#define STEP 0x071 948c2ecf20Sopenharmony_ci#define MOTOR_ON 0x072 958c2ecf20Sopenharmony_ci#define MOTOR_OFF 0x076 968c2ecf20Sopenharmony_ci#define INDEX 0x073 978c2ecf20Sopenharmony_ci#define EJECT 0x077 988c2ecf20Sopenharmony_ci#define SETMFM 0x171 998c2ecf20Sopenharmony_ci#define SETGCR 0x175 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci#define RELAX 0x033 1028c2ecf20Sopenharmony_ci#define LSTRB 0x008 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci#define CA_MASK 0x077 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci/* Select values for swim_select and swim_readbit */ 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci#define READ_DATA_0 0x074 1098c2ecf20Sopenharmony_ci#define ONEMEG_DRIVE 0x075 1108c2ecf20Sopenharmony_ci#define SINGLE_SIDED 0x076 1118c2ecf20Sopenharmony_ci#define DRIVE_PRESENT 0x077 1128c2ecf20Sopenharmony_ci#define DISK_IN 0x170 1138c2ecf20Sopenharmony_ci#define WRITE_PROT 0x171 1148c2ecf20Sopenharmony_ci#define TRACK_ZERO 0x172 1158c2ecf20Sopenharmony_ci#define TACHO 0x173 1168c2ecf20Sopenharmony_ci#define READ_DATA_1 0x174 1178c2ecf20Sopenharmony_ci#define GCR_MODE 0x175 1188c2ecf20Sopenharmony_ci#define SEEK_COMPLETE 0x176 1198c2ecf20Sopenharmony_ci#define TWOMEG_MEDIA 0x177 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci/* Bits in handshake register */ 1228c2ecf20Sopenharmony_ci 1238c2ecf20Sopenharmony_ci#define MARK_BYTE 0x01 1248c2ecf20Sopenharmony_ci#define CRC_ZERO 0x02 1258c2ecf20Sopenharmony_ci#define RDDATA 0x04 1268c2ecf20Sopenharmony_ci#define SENSE 0x08 1278c2ecf20Sopenharmony_ci#define MOTEN 0x10 1288c2ecf20Sopenharmony_ci#define ERROR 0x20 1298c2ecf20Sopenharmony_ci#define DAT2BYTE 0x40 1308c2ecf20Sopenharmony_ci#define DAT1BYTE 0x80 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci/* bits in setup register */ 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ci#define S_INV_WDATA 0x01 1358c2ecf20Sopenharmony_ci#define S_3_5_SELECT 0x02 1368c2ecf20Sopenharmony_ci#define S_GCR 0x04 1378c2ecf20Sopenharmony_ci#define S_FCLK_DIV2 0x08 1388c2ecf20Sopenharmony_ci#define S_ERROR_CORR 0x10 1398c2ecf20Sopenharmony_ci#define S_IBM_DRIVE 0x20 1408c2ecf20Sopenharmony_ci#define S_GCR_WRITE 0x40 1418c2ecf20Sopenharmony_ci#define S_TIMEOUT 0x80 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci/* bits in mode register */ 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci#define CLFIFO 0x01 1468c2ecf20Sopenharmony_ci#define ENBL1 0x02 1478c2ecf20Sopenharmony_ci#define ENBL2 0x04 1488c2ecf20Sopenharmony_ci#define ACTION 0x08 1498c2ecf20Sopenharmony_ci#define WRITE_MODE 0x10 1508c2ecf20Sopenharmony_ci#define HEDSEL 0x20 1518c2ecf20Sopenharmony_ci#define MOTON 0x80 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci/*----------------------------------------------------------------------------*/ 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_cienum drive_location { 1568c2ecf20Sopenharmony_ci INTERNAL_DRIVE = 0x02, 1578c2ecf20Sopenharmony_ci EXTERNAL_DRIVE = 0x04, 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_cienum media_type { 1618c2ecf20Sopenharmony_ci DD_MEDIA, 1628c2ecf20Sopenharmony_ci HD_MEDIA, 1638c2ecf20Sopenharmony_ci}; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_cistruct floppy_state { 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_ci /* physical properties */ 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci enum drive_location location; /* internal or external drive */ 1708c2ecf20Sopenharmony_ci int head_number; /* single- or double-sided drive */ 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* media */ 1738c2ecf20Sopenharmony_ci 1748c2ecf20Sopenharmony_ci int disk_in; 1758c2ecf20Sopenharmony_ci int ejected; 1768c2ecf20Sopenharmony_ci enum media_type type; 1778c2ecf20Sopenharmony_ci int write_protected; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci int total_secs; 1808c2ecf20Sopenharmony_ci int secpercyl; 1818c2ecf20Sopenharmony_ci int secpertrack; 1828c2ecf20Sopenharmony_ci 1838c2ecf20Sopenharmony_ci /* in-use information */ 1848c2ecf20Sopenharmony_ci 1858c2ecf20Sopenharmony_ci int track; 1868c2ecf20Sopenharmony_ci int ref_count; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci struct gendisk *disk; 1898c2ecf20Sopenharmony_ci struct blk_mq_tag_set tag_set; 1908c2ecf20Sopenharmony_ci 1918c2ecf20Sopenharmony_ci /* parent controller */ 1928c2ecf20Sopenharmony_ci 1938c2ecf20Sopenharmony_ci struct swim_priv *swd; 1948c2ecf20Sopenharmony_ci}; 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_cienum motor_action { 1978c2ecf20Sopenharmony_ci OFF, 1988c2ecf20Sopenharmony_ci ON, 1998c2ecf20Sopenharmony_ci}; 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_cienum head { 2028c2ecf20Sopenharmony_ci LOWER_HEAD = 0, 2038c2ecf20Sopenharmony_ci UPPER_HEAD = 1, 2048c2ecf20Sopenharmony_ci}; 2058c2ecf20Sopenharmony_ci 2068c2ecf20Sopenharmony_ci#define FD_MAX_UNIT 2 2078c2ecf20Sopenharmony_ci 2088c2ecf20Sopenharmony_cistruct swim_priv { 2098c2ecf20Sopenharmony_ci struct swim __iomem *base; 2108c2ecf20Sopenharmony_ci spinlock_t lock; 2118c2ecf20Sopenharmony_ci int floppy_count; 2128c2ecf20Sopenharmony_ci struct floppy_state unit[FD_MAX_UNIT]; 2138c2ecf20Sopenharmony_ci}; 2148c2ecf20Sopenharmony_ci 2158c2ecf20Sopenharmony_ciextern int swim_read_sector_header(struct swim __iomem *base, 2168c2ecf20Sopenharmony_ci struct sector_header *header); 2178c2ecf20Sopenharmony_ciextern int swim_read_sector_data(struct swim __iomem *base, 2188c2ecf20Sopenharmony_ci unsigned char *data); 2198c2ecf20Sopenharmony_ci 2208c2ecf20Sopenharmony_cistatic DEFINE_MUTEX(swim_mutex); 2218c2ecf20Sopenharmony_cistatic inline void set_swim_mode(struct swim __iomem *base, int enable) 2228c2ecf20Sopenharmony_ci{ 2238c2ecf20Sopenharmony_ci struct iwm __iomem *iwm_base; 2248c2ecf20Sopenharmony_ci unsigned long flags; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci if (!enable) { 2278c2ecf20Sopenharmony_ci swim_write(base, mode0, 0xf8); 2288c2ecf20Sopenharmony_ci return; 2298c2ecf20Sopenharmony_ci } 2308c2ecf20Sopenharmony_ci 2318c2ecf20Sopenharmony_ci iwm_base = (struct iwm __iomem *)base; 2328c2ecf20Sopenharmony_ci local_irq_save(flags); 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci iwm_read(iwm_base, q7L); 2358c2ecf20Sopenharmony_ci iwm_read(iwm_base, mtrOff); 2368c2ecf20Sopenharmony_ci iwm_read(iwm_base, q6H); 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci iwm_write(iwm_base, q7H, 0x57); 2398c2ecf20Sopenharmony_ci iwm_write(iwm_base, q7H, 0x17); 2408c2ecf20Sopenharmony_ci iwm_write(iwm_base, q7H, 0x57); 2418c2ecf20Sopenharmony_ci iwm_write(iwm_base, q7H, 0x57); 2428c2ecf20Sopenharmony_ci 2438c2ecf20Sopenharmony_ci local_irq_restore(flags); 2448c2ecf20Sopenharmony_ci} 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_cistatic inline int get_swim_mode(struct swim __iomem *base) 2478c2ecf20Sopenharmony_ci{ 2488c2ecf20Sopenharmony_ci unsigned long flags; 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci local_irq_save(flags); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ci swim_write(base, phase, 0xf5); 2538c2ecf20Sopenharmony_ci if (swim_read(base, phase) != 0xf5) 2548c2ecf20Sopenharmony_ci goto is_iwm; 2558c2ecf20Sopenharmony_ci swim_write(base, phase, 0xf6); 2568c2ecf20Sopenharmony_ci if (swim_read(base, phase) != 0xf6) 2578c2ecf20Sopenharmony_ci goto is_iwm; 2588c2ecf20Sopenharmony_ci swim_write(base, phase, 0xf7); 2598c2ecf20Sopenharmony_ci if (swim_read(base, phase) != 0xf7) 2608c2ecf20Sopenharmony_ci goto is_iwm; 2618c2ecf20Sopenharmony_ci local_irq_restore(flags); 2628c2ecf20Sopenharmony_ci return 1; 2638c2ecf20Sopenharmony_ciis_iwm: 2648c2ecf20Sopenharmony_ci local_irq_restore(flags); 2658c2ecf20Sopenharmony_ci return 0; 2668c2ecf20Sopenharmony_ci} 2678c2ecf20Sopenharmony_ci 2688c2ecf20Sopenharmony_cistatic inline void swim_select(struct swim __iomem *base, int sel) 2698c2ecf20Sopenharmony_ci{ 2708c2ecf20Sopenharmony_ci swim_write(base, phase, RELAX); 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci via1_set_head(sel & 0x100); 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci swim_write(base, phase, sel & CA_MASK); 2758c2ecf20Sopenharmony_ci} 2768c2ecf20Sopenharmony_ci 2778c2ecf20Sopenharmony_cistatic inline void swim_action(struct swim __iomem *base, int action) 2788c2ecf20Sopenharmony_ci{ 2798c2ecf20Sopenharmony_ci unsigned long flags; 2808c2ecf20Sopenharmony_ci 2818c2ecf20Sopenharmony_ci local_irq_save(flags); 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci swim_select(base, action); 2848c2ecf20Sopenharmony_ci udelay(1); 2858c2ecf20Sopenharmony_ci swim_write(base, phase, (LSTRB<<4) | LSTRB); 2868c2ecf20Sopenharmony_ci udelay(1); 2878c2ecf20Sopenharmony_ci swim_write(base, phase, (LSTRB<<4) | ((~LSTRB) & 0x0F)); 2888c2ecf20Sopenharmony_ci udelay(1); 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci local_irq_restore(flags); 2918c2ecf20Sopenharmony_ci} 2928c2ecf20Sopenharmony_ci 2938c2ecf20Sopenharmony_cistatic inline int swim_readbit(struct swim __iomem *base, int bit) 2948c2ecf20Sopenharmony_ci{ 2958c2ecf20Sopenharmony_ci int stat; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ci swim_select(base, bit); 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci udelay(10); 3008c2ecf20Sopenharmony_ci 3018c2ecf20Sopenharmony_ci stat = swim_read(base, handshake); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci return (stat & SENSE) == 0; 3048c2ecf20Sopenharmony_ci} 3058c2ecf20Sopenharmony_ci 3068c2ecf20Sopenharmony_cistatic inline void swim_drive(struct swim __iomem *base, 3078c2ecf20Sopenharmony_ci enum drive_location location) 3088c2ecf20Sopenharmony_ci{ 3098c2ecf20Sopenharmony_ci if (location == INTERNAL_DRIVE) { 3108c2ecf20Sopenharmony_ci swim_write(base, mode0, EXTERNAL_DRIVE); /* clear drive 1 bit */ 3118c2ecf20Sopenharmony_ci swim_write(base, mode1, INTERNAL_DRIVE); /* set drive 0 bit */ 3128c2ecf20Sopenharmony_ci } else if (location == EXTERNAL_DRIVE) { 3138c2ecf20Sopenharmony_ci swim_write(base, mode0, INTERNAL_DRIVE); /* clear drive 0 bit */ 3148c2ecf20Sopenharmony_ci swim_write(base, mode1, EXTERNAL_DRIVE); /* set drive 1 bit */ 3158c2ecf20Sopenharmony_ci } 3168c2ecf20Sopenharmony_ci} 3178c2ecf20Sopenharmony_ci 3188c2ecf20Sopenharmony_cistatic inline void swim_motor(struct swim __iomem *base, 3198c2ecf20Sopenharmony_ci enum motor_action action) 3208c2ecf20Sopenharmony_ci{ 3218c2ecf20Sopenharmony_ci if (action == ON) { 3228c2ecf20Sopenharmony_ci int i; 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci swim_action(base, MOTOR_ON); 3258c2ecf20Sopenharmony_ci 3268c2ecf20Sopenharmony_ci for (i = 0; i < 2*HZ; i++) { 3278c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3288c2ecf20Sopenharmony_ci if (swim_readbit(base, MOTOR_ON)) 3298c2ecf20Sopenharmony_ci break; 3308c2ecf20Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 3318c2ecf20Sopenharmony_ci schedule_timeout(1); 3328c2ecf20Sopenharmony_ci } 3338c2ecf20Sopenharmony_ci } else if (action == OFF) { 3348c2ecf20Sopenharmony_ci swim_action(base, MOTOR_OFF); 3358c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3368c2ecf20Sopenharmony_ci } 3378c2ecf20Sopenharmony_ci} 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_cistatic inline void swim_eject(struct swim __iomem *base) 3408c2ecf20Sopenharmony_ci{ 3418c2ecf20Sopenharmony_ci int i; 3428c2ecf20Sopenharmony_ci 3438c2ecf20Sopenharmony_ci swim_action(base, EJECT); 3448c2ecf20Sopenharmony_ci 3458c2ecf20Sopenharmony_ci for (i = 0; i < 2*HZ; i++) { 3468c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3478c2ecf20Sopenharmony_ci if (!swim_readbit(base, DISK_IN)) 3488c2ecf20Sopenharmony_ci break; 3498c2ecf20Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 3508c2ecf20Sopenharmony_ci schedule_timeout(1); 3518c2ecf20Sopenharmony_ci } 3528c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3538c2ecf20Sopenharmony_ci} 3548c2ecf20Sopenharmony_ci 3558c2ecf20Sopenharmony_cistatic inline void swim_head(struct swim __iomem *base, enum head head) 3568c2ecf20Sopenharmony_ci{ 3578c2ecf20Sopenharmony_ci /* wait drive is ready */ 3588c2ecf20Sopenharmony_ci 3598c2ecf20Sopenharmony_ci if (head == UPPER_HEAD) 3608c2ecf20Sopenharmony_ci swim_select(base, READ_DATA_1); 3618c2ecf20Sopenharmony_ci else if (head == LOWER_HEAD) 3628c2ecf20Sopenharmony_ci swim_select(base, READ_DATA_0); 3638c2ecf20Sopenharmony_ci} 3648c2ecf20Sopenharmony_ci 3658c2ecf20Sopenharmony_cistatic inline int swim_step(struct swim __iomem *base) 3668c2ecf20Sopenharmony_ci{ 3678c2ecf20Sopenharmony_ci int wait; 3688c2ecf20Sopenharmony_ci 3698c2ecf20Sopenharmony_ci swim_action(base, STEP); 3708c2ecf20Sopenharmony_ci 3718c2ecf20Sopenharmony_ci for (wait = 0; wait < HZ; wait++) { 3728c2ecf20Sopenharmony_ci 3738c2ecf20Sopenharmony_ci set_current_state(TASK_INTERRUPTIBLE); 3748c2ecf20Sopenharmony_ci schedule_timeout(1); 3758c2ecf20Sopenharmony_ci 3768c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3778c2ecf20Sopenharmony_ci if (!swim_readbit(base, STEP)) 3788c2ecf20Sopenharmony_ci return 0; 3798c2ecf20Sopenharmony_ci } 3808c2ecf20Sopenharmony_ci return -1; 3818c2ecf20Sopenharmony_ci} 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_cistatic inline int swim_track00(struct swim __iomem *base) 3848c2ecf20Sopenharmony_ci{ 3858c2ecf20Sopenharmony_ci int try; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci swim_action(base, SEEK_NEGATIVE); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ci for (try = 0; try < 100; try++) { 3908c2ecf20Sopenharmony_ci 3918c2ecf20Sopenharmony_ci swim_select(base, RELAX); 3928c2ecf20Sopenharmony_ci if (swim_readbit(base, TRACK_ZERO)) 3938c2ecf20Sopenharmony_ci break; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_ci if (swim_step(base)) 3968c2ecf20Sopenharmony_ci return -1; 3978c2ecf20Sopenharmony_ci } 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci if (swim_readbit(base, TRACK_ZERO)) 4008c2ecf20Sopenharmony_ci return 0; 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ci return -1; 4038c2ecf20Sopenharmony_ci} 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_cistatic inline int swim_seek(struct swim __iomem *base, int step) 4068c2ecf20Sopenharmony_ci{ 4078c2ecf20Sopenharmony_ci if (step == 0) 4088c2ecf20Sopenharmony_ci return 0; 4098c2ecf20Sopenharmony_ci 4108c2ecf20Sopenharmony_ci if (step < 0) { 4118c2ecf20Sopenharmony_ci swim_action(base, SEEK_NEGATIVE); 4128c2ecf20Sopenharmony_ci step = -step; 4138c2ecf20Sopenharmony_ci } else 4148c2ecf20Sopenharmony_ci swim_action(base, SEEK_POSITIVE); 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci for ( ; step > 0; step--) { 4178c2ecf20Sopenharmony_ci if (swim_step(base)) 4188c2ecf20Sopenharmony_ci return -1; 4198c2ecf20Sopenharmony_ci } 4208c2ecf20Sopenharmony_ci 4218c2ecf20Sopenharmony_ci return 0; 4228c2ecf20Sopenharmony_ci} 4238c2ecf20Sopenharmony_ci 4248c2ecf20Sopenharmony_cistatic inline int swim_track(struct floppy_state *fs, int track) 4258c2ecf20Sopenharmony_ci{ 4268c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 4278c2ecf20Sopenharmony_ci int ret; 4288c2ecf20Sopenharmony_ci 4298c2ecf20Sopenharmony_ci ret = swim_seek(base, track - fs->track); 4308c2ecf20Sopenharmony_ci 4318c2ecf20Sopenharmony_ci if (ret == 0) 4328c2ecf20Sopenharmony_ci fs->track = track; 4338c2ecf20Sopenharmony_ci else { 4348c2ecf20Sopenharmony_ci swim_track00(base); 4358c2ecf20Sopenharmony_ci fs->track = 0; 4368c2ecf20Sopenharmony_ci } 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci return ret; 4398c2ecf20Sopenharmony_ci} 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_cistatic int floppy_eject(struct floppy_state *fs) 4428c2ecf20Sopenharmony_ci{ 4438c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 4448c2ecf20Sopenharmony_ci 4458c2ecf20Sopenharmony_ci swim_drive(base, fs->location); 4468c2ecf20Sopenharmony_ci swim_motor(base, OFF); 4478c2ecf20Sopenharmony_ci swim_eject(base); 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci fs->disk_in = 0; 4508c2ecf20Sopenharmony_ci fs->ejected = 1; 4518c2ecf20Sopenharmony_ci 4528c2ecf20Sopenharmony_ci return 0; 4538c2ecf20Sopenharmony_ci} 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_cistatic inline int swim_read_sector(struct floppy_state *fs, 4568c2ecf20Sopenharmony_ci int side, int track, 4578c2ecf20Sopenharmony_ci int sector, unsigned char *buffer) 4588c2ecf20Sopenharmony_ci{ 4598c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 4608c2ecf20Sopenharmony_ci unsigned long flags; 4618c2ecf20Sopenharmony_ci struct sector_header header; 4628c2ecf20Sopenharmony_ci int ret = -1; 4638c2ecf20Sopenharmony_ci short i; 4648c2ecf20Sopenharmony_ci 4658c2ecf20Sopenharmony_ci swim_track(fs, track); 4668c2ecf20Sopenharmony_ci 4678c2ecf20Sopenharmony_ci swim_write(base, mode1, MOTON); 4688c2ecf20Sopenharmony_ci swim_head(base, side); 4698c2ecf20Sopenharmony_ci swim_write(base, mode0, side); 4708c2ecf20Sopenharmony_ci 4718c2ecf20Sopenharmony_ci local_irq_save(flags); 4728c2ecf20Sopenharmony_ci for (i = 0; i < 36; i++) { 4738c2ecf20Sopenharmony_ci ret = swim_read_sector_header(base, &header); 4748c2ecf20Sopenharmony_ci if (!ret && (header.sector == sector)) { 4758c2ecf20Sopenharmony_ci /* found */ 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_ci ret = swim_read_sector_data(base, buffer); 4788c2ecf20Sopenharmony_ci break; 4798c2ecf20Sopenharmony_ci } 4808c2ecf20Sopenharmony_ci } 4818c2ecf20Sopenharmony_ci local_irq_restore(flags); 4828c2ecf20Sopenharmony_ci 4838c2ecf20Sopenharmony_ci swim_write(base, mode0, MOTON); 4848c2ecf20Sopenharmony_ci 4858c2ecf20Sopenharmony_ci if ((header.side != side) || (header.track != track) || 4868c2ecf20Sopenharmony_ci (header.sector != sector)) 4878c2ecf20Sopenharmony_ci return 0; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_ci return ret; 4908c2ecf20Sopenharmony_ci} 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_cistatic blk_status_t floppy_read_sectors(struct floppy_state *fs, 4938c2ecf20Sopenharmony_ci int req_sector, int sectors_nb, 4948c2ecf20Sopenharmony_ci unsigned char *buffer) 4958c2ecf20Sopenharmony_ci{ 4968c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 4978c2ecf20Sopenharmony_ci int ret; 4988c2ecf20Sopenharmony_ci int side, track, sector; 4998c2ecf20Sopenharmony_ci int i, try; 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci 5028c2ecf20Sopenharmony_ci swim_drive(base, fs->location); 5038c2ecf20Sopenharmony_ci for (i = req_sector; i < req_sector + sectors_nb; i++) { 5048c2ecf20Sopenharmony_ci int x; 5058c2ecf20Sopenharmony_ci track = i / fs->secpercyl; 5068c2ecf20Sopenharmony_ci x = i % fs->secpercyl; 5078c2ecf20Sopenharmony_ci side = x / fs->secpertrack; 5088c2ecf20Sopenharmony_ci sector = x % fs->secpertrack + 1; 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci try = 5; 5118c2ecf20Sopenharmony_ci do { 5128c2ecf20Sopenharmony_ci ret = swim_read_sector(fs, side, track, sector, 5138c2ecf20Sopenharmony_ci buffer); 5148c2ecf20Sopenharmony_ci if (try-- == 0) 5158c2ecf20Sopenharmony_ci return BLK_STS_IOERR; 5168c2ecf20Sopenharmony_ci } while (ret != 512); 5178c2ecf20Sopenharmony_ci 5188c2ecf20Sopenharmony_ci buffer += ret; 5198c2ecf20Sopenharmony_ci } 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci return 0; 5228c2ecf20Sopenharmony_ci} 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_cistatic blk_status_t swim_queue_rq(struct blk_mq_hw_ctx *hctx, 5258c2ecf20Sopenharmony_ci const struct blk_mq_queue_data *bd) 5268c2ecf20Sopenharmony_ci{ 5278c2ecf20Sopenharmony_ci struct floppy_state *fs = hctx->queue->queuedata; 5288c2ecf20Sopenharmony_ci struct swim_priv *swd = fs->swd; 5298c2ecf20Sopenharmony_ci struct request *req = bd->rq; 5308c2ecf20Sopenharmony_ci blk_status_t err; 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci if (!spin_trylock_irq(&swd->lock)) 5338c2ecf20Sopenharmony_ci return BLK_STS_DEV_RESOURCE; 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci blk_mq_start_request(req); 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci if (!fs->disk_in || rq_data_dir(req) == WRITE) { 5388c2ecf20Sopenharmony_ci err = BLK_STS_IOERR; 5398c2ecf20Sopenharmony_ci goto out; 5408c2ecf20Sopenharmony_ci } 5418c2ecf20Sopenharmony_ci 5428c2ecf20Sopenharmony_ci do { 5438c2ecf20Sopenharmony_ci err = floppy_read_sectors(fs, blk_rq_pos(req), 5448c2ecf20Sopenharmony_ci blk_rq_cur_sectors(req), 5458c2ecf20Sopenharmony_ci bio_data(req->bio)); 5468c2ecf20Sopenharmony_ci } while (blk_update_request(req, err, blk_rq_cur_bytes(req))); 5478c2ecf20Sopenharmony_ci __blk_mq_end_request(req, err); 5488c2ecf20Sopenharmony_ci 5498c2ecf20Sopenharmony_ci err = BLK_STS_OK; 5508c2ecf20Sopenharmony_ciout: 5518c2ecf20Sopenharmony_ci spin_unlock_irq(&swd->lock); 5528c2ecf20Sopenharmony_ci return err; 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_ci} 5558c2ecf20Sopenharmony_ci 5568c2ecf20Sopenharmony_cistatic struct floppy_struct floppy_type[4] = { 5578c2ecf20Sopenharmony_ci { 0, 0, 0, 0, 0, 0x00, 0x00, 0x00, 0x00, NULL }, /* no testing */ 5588c2ecf20Sopenharmony_ci { 720, 9, 1, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 360KB SS 3.5"*/ 5598c2ecf20Sopenharmony_ci { 1440, 9, 2, 80, 0, 0x2A, 0x02, 0xDF, 0x50, NULL }, /* 720KB 3.5" */ 5608c2ecf20Sopenharmony_ci { 2880, 18, 2, 80, 0, 0x1B, 0x00, 0xCF, 0x6C, NULL }, /* 1.44MB 3.5" */ 5618c2ecf20Sopenharmony_ci}; 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_cistatic int get_floppy_geometry(struct floppy_state *fs, int type, 5648c2ecf20Sopenharmony_ci struct floppy_struct **g) 5658c2ecf20Sopenharmony_ci{ 5668c2ecf20Sopenharmony_ci if (type >= ARRAY_SIZE(floppy_type)) 5678c2ecf20Sopenharmony_ci return -EINVAL; 5688c2ecf20Sopenharmony_ci 5698c2ecf20Sopenharmony_ci if (type) 5708c2ecf20Sopenharmony_ci *g = &floppy_type[type]; 5718c2ecf20Sopenharmony_ci else if (fs->type == HD_MEDIA) /* High-Density media */ 5728c2ecf20Sopenharmony_ci *g = &floppy_type[3]; 5738c2ecf20Sopenharmony_ci else if (fs->head_number == 2) /* double-sided */ 5748c2ecf20Sopenharmony_ci *g = &floppy_type[2]; 5758c2ecf20Sopenharmony_ci else 5768c2ecf20Sopenharmony_ci *g = &floppy_type[1]; 5778c2ecf20Sopenharmony_ci 5788c2ecf20Sopenharmony_ci return 0; 5798c2ecf20Sopenharmony_ci} 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_cistatic void setup_medium(struct floppy_state *fs) 5828c2ecf20Sopenharmony_ci{ 5838c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 5848c2ecf20Sopenharmony_ci 5858c2ecf20Sopenharmony_ci if (swim_readbit(base, DISK_IN)) { 5868c2ecf20Sopenharmony_ci struct floppy_struct *g; 5878c2ecf20Sopenharmony_ci fs->disk_in = 1; 5888c2ecf20Sopenharmony_ci fs->write_protected = swim_readbit(base, WRITE_PROT); 5898c2ecf20Sopenharmony_ci 5908c2ecf20Sopenharmony_ci if (swim_track00(base)) 5918c2ecf20Sopenharmony_ci printk(KERN_ERR 5928c2ecf20Sopenharmony_ci "SWIM: cannot move floppy head to track 0\n"); 5938c2ecf20Sopenharmony_ci 5948c2ecf20Sopenharmony_ci swim_track00(base); 5958c2ecf20Sopenharmony_ci 5968c2ecf20Sopenharmony_ci fs->type = swim_readbit(base, TWOMEG_MEDIA) ? 5978c2ecf20Sopenharmony_ci HD_MEDIA : DD_MEDIA; 5988c2ecf20Sopenharmony_ci fs->head_number = swim_readbit(base, SINGLE_SIDED) ? 1 : 2; 5998c2ecf20Sopenharmony_ci get_floppy_geometry(fs, 0, &g); 6008c2ecf20Sopenharmony_ci fs->total_secs = g->size; 6018c2ecf20Sopenharmony_ci fs->secpercyl = g->head * g->sect; 6028c2ecf20Sopenharmony_ci fs->secpertrack = g->sect; 6038c2ecf20Sopenharmony_ci fs->track = 0; 6048c2ecf20Sopenharmony_ci } else { 6058c2ecf20Sopenharmony_ci fs->disk_in = 0; 6068c2ecf20Sopenharmony_ci } 6078c2ecf20Sopenharmony_ci} 6088c2ecf20Sopenharmony_ci 6098c2ecf20Sopenharmony_cistatic int floppy_open(struct block_device *bdev, fmode_t mode) 6108c2ecf20Sopenharmony_ci{ 6118c2ecf20Sopenharmony_ci struct floppy_state *fs = bdev->bd_disk->private_data; 6128c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 6138c2ecf20Sopenharmony_ci int err; 6148c2ecf20Sopenharmony_ci 6158c2ecf20Sopenharmony_ci if (fs->ref_count == -1 || (fs->ref_count && mode & FMODE_EXCL)) 6168c2ecf20Sopenharmony_ci return -EBUSY; 6178c2ecf20Sopenharmony_ci 6188c2ecf20Sopenharmony_ci if (mode & FMODE_EXCL) 6198c2ecf20Sopenharmony_ci fs->ref_count = -1; 6208c2ecf20Sopenharmony_ci else 6218c2ecf20Sopenharmony_ci fs->ref_count++; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci swim_write(base, setup, S_IBM_DRIVE | S_FCLK_DIV2); 6248c2ecf20Sopenharmony_ci udelay(10); 6258c2ecf20Sopenharmony_ci swim_drive(base, fs->location); 6268c2ecf20Sopenharmony_ci swim_motor(base, ON); 6278c2ecf20Sopenharmony_ci swim_action(base, SETMFM); 6288c2ecf20Sopenharmony_ci if (fs->ejected) 6298c2ecf20Sopenharmony_ci setup_medium(fs); 6308c2ecf20Sopenharmony_ci if (!fs->disk_in) { 6318c2ecf20Sopenharmony_ci err = -ENXIO; 6328c2ecf20Sopenharmony_ci goto out; 6338c2ecf20Sopenharmony_ci } 6348c2ecf20Sopenharmony_ci 6358c2ecf20Sopenharmony_ci set_capacity(fs->disk, fs->total_secs); 6368c2ecf20Sopenharmony_ci 6378c2ecf20Sopenharmony_ci if (mode & FMODE_NDELAY) 6388c2ecf20Sopenharmony_ci return 0; 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci if (mode & (FMODE_READ|FMODE_WRITE)) { 6418c2ecf20Sopenharmony_ci if (bdev_check_media_change(bdev) && fs->disk_in) 6428c2ecf20Sopenharmony_ci fs->ejected = 0; 6438c2ecf20Sopenharmony_ci if ((mode & FMODE_WRITE) && fs->write_protected) { 6448c2ecf20Sopenharmony_ci err = -EROFS; 6458c2ecf20Sopenharmony_ci goto out; 6468c2ecf20Sopenharmony_ci } 6478c2ecf20Sopenharmony_ci } 6488c2ecf20Sopenharmony_ci return 0; 6498c2ecf20Sopenharmony_ciout: 6508c2ecf20Sopenharmony_ci if (fs->ref_count < 0) 6518c2ecf20Sopenharmony_ci fs->ref_count = 0; 6528c2ecf20Sopenharmony_ci else if (fs->ref_count > 0) 6538c2ecf20Sopenharmony_ci --fs->ref_count; 6548c2ecf20Sopenharmony_ci 6558c2ecf20Sopenharmony_ci if (fs->ref_count == 0) 6568c2ecf20Sopenharmony_ci swim_motor(base, OFF); 6578c2ecf20Sopenharmony_ci return err; 6588c2ecf20Sopenharmony_ci} 6598c2ecf20Sopenharmony_ci 6608c2ecf20Sopenharmony_cistatic int floppy_unlocked_open(struct block_device *bdev, fmode_t mode) 6618c2ecf20Sopenharmony_ci{ 6628c2ecf20Sopenharmony_ci int ret; 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci mutex_lock(&swim_mutex); 6658c2ecf20Sopenharmony_ci ret = floppy_open(bdev, mode); 6668c2ecf20Sopenharmony_ci mutex_unlock(&swim_mutex); 6678c2ecf20Sopenharmony_ci 6688c2ecf20Sopenharmony_ci return ret; 6698c2ecf20Sopenharmony_ci} 6708c2ecf20Sopenharmony_ci 6718c2ecf20Sopenharmony_cistatic void floppy_release(struct gendisk *disk, fmode_t mode) 6728c2ecf20Sopenharmony_ci{ 6738c2ecf20Sopenharmony_ci struct floppy_state *fs = disk->private_data; 6748c2ecf20Sopenharmony_ci struct swim __iomem *base = fs->swd->base; 6758c2ecf20Sopenharmony_ci 6768c2ecf20Sopenharmony_ci mutex_lock(&swim_mutex); 6778c2ecf20Sopenharmony_ci if (fs->ref_count < 0) 6788c2ecf20Sopenharmony_ci fs->ref_count = 0; 6798c2ecf20Sopenharmony_ci else if (fs->ref_count > 0) 6808c2ecf20Sopenharmony_ci --fs->ref_count; 6818c2ecf20Sopenharmony_ci 6828c2ecf20Sopenharmony_ci if (fs->ref_count == 0) 6838c2ecf20Sopenharmony_ci swim_motor(base, OFF); 6848c2ecf20Sopenharmony_ci mutex_unlock(&swim_mutex); 6858c2ecf20Sopenharmony_ci} 6868c2ecf20Sopenharmony_ci 6878c2ecf20Sopenharmony_cistatic int floppy_ioctl(struct block_device *bdev, fmode_t mode, 6888c2ecf20Sopenharmony_ci unsigned int cmd, unsigned long param) 6898c2ecf20Sopenharmony_ci{ 6908c2ecf20Sopenharmony_ci struct floppy_state *fs = bdev->bd_disk->private_data; 6918c2ecf20Sopenharmony_ci int err; 6928c2ecf20Sopenharmony_ci 6938c2ecf20Sopenharmony_ci if ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)) 6948c2ecf20Sopenharmony_ci return -EPERM; 6958c2ecf20Sopenharmony_ci 6968c2ecf20Sopenharmony_ci switch (cmd) { 6978c2ecf20Sopenharmony_ci case FDEJECT: 6988c2ecf20Sopenharmony_ci if (fs->ref_count != 1) 6998c2ecf20Sopenharmony_ci return -EBUSY; 7008c2ecf20Sopenharmony_ci mutex_lock(&swim_mutex); 7018c2ecf20Sopenharmony_ci err = floppy_eject(fs); 7028c2ecf20Sopenharmony_ci mutex_unlock(&swim_mutex); 7038c2ecf20Sopenharmony_ci return err; 7048c2ecf20Sopenharmony_ci 7058c2ecf20Sopenharmony_ci case FDGETPRM: 7068c2ecf20Sopenharmony_ci if (copy_to_user((void __user *) param, (void *) &floppy_type, 7078c2ecf20Sopenharmony_ci sizeof(struct floppy_struct))) 7088c2ecf20Sopenharmony_ci return -EFAULT; 7098c2ecf20Sopenharmony_ci return 0; 7108c2ecf20Sopenharmony_ci } 7118c2ecf20Sopenharmony_ci return -ENOTTY; 7128c2ecf20Sopenharmony_ci} 7138c2ecf20Sopenharmony_ci 7148c2ecf20Sopenharmony_cistatic int floppy_getgeo(struct block_device *bdev, struct hd_geometry *geo) 7158c2ecf20Sopenharmony_ci{ 7168c2ecf20Sopenharmony_ci struct floppy_state *fs = bdev->bd_disk->private_data; 7178c2ecf20Sopenharmony_ci struct floppy_struct *g; 7188c2ecf20Sopenharmony_ci int ret; 7198c2ecf20Sopenharmony_ci 7208c2ecf20Sopenharmony_ci ret = get_floppy_geometry(fs, 0, &g); 7218c2ecf20Sopenharmony_ci if (ret) 7228c2ecf20Sopenharmony_ci return ret; 7238c2ecf20Sopenharmony_ci 7248c2ecf20Sopenharmony_ci geo->heads = g->head; 7258c2ecf20Sopenharmony_ci geo->sectors = g->sect; 7268c2ecf20Sopenharmony_ci geo->cylinders = g->track; 7278c2ecf20Sopenharmony_ci 7288c2ecf20Sopenharmony_ci return 0; 7298c2ecf20Sopenharmony_ci} 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_cistatic unsigned int floppy_check_events(struct gendisk *disk, 7328c2ecf20Sopenharmony_ci unsigned int clearing) 7338c2ecf20Sopenharmony_ci{ 7348c2ecf20Sopenharmony_ci struct floppy_state *fs = disk->private_data; 7358c2ecf20Sopenharmony_ci 7368c2ecf20Sopenharmony_ci return fs->ejected ? DISK_EVENT_MEDIA_CHANGE : 0; 7378c2ecf20Sopenharmony_ci} 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_cistatic const struct block_device_operations floppy_fops = { 7408c2ecf20Sopenharmony_ci .owner = THIS_MODULE, 7418c2ecf20Sopenharmony_ci .open = floppy_unlocked_open, 7428c2ecf20Sopenharmony_ci .release = floppy_release, 7438c2ecf20Sopenharmony_ci .ioctl = floppy_ioctl, 7448c2ecf20Sopenharmony_ci .getgeo = floppy_getgeo, 7458c2ecf20Sopenharmony_ci .check_events = floppy_check_events, 7468c2ecf20Sopenharmony_ci}; 7478c2ecf20Sopenharmony_ci 7488c2ecf20Sopenharmony_cistatic struct kobject *floppy_find(dev_t dev, int *part, void *data) 7498c2ecf20Sopenharmony_ci{ 7508c2ecf20Sopenharmony_ci struct swim_priv *swd = data; 7518c2ecf20Sopenharmony_ci int drive = (*part & 3); 7528c2ecf20Sopenharmony_ci 7538c2ecf20Sopenharmony_ci if (drive >= swd->floppy_count) 7548c2ecf20Sopenharmony_ci return NULL; 7558c2ecf20Sopenharmony_ci 7568c2ecf20Sopenharmony_ci *part = 0; 7578c2ecf20Sopenharmony_ci return get_disk_and_module(swd->unit[drive].disk); 7588c2ecf20Sopenharmony_ci} 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_cistatic int swim_add_floppy(struct swim_priv *swd, enum drive_location location) 7618c2ecf20Sopenharmony_ci{ 7628c2ecf20Sopenharmony_ci struct floppy_state *fs = &swd->unit[swd->floppy_count]; 7638c2ecf20Sopenharmony_ci struct swim __iomem *base = swd->base; 7648c2ecf20Sopenharmony_ci 7658c2ecf20Sopenharmony_ci fs->location = location; 7668c2ecf20Sopenharmony_ci 7678c2ecf20Sopenharmony_ci swim_drive(base, location); 7688c2ecf20Sopenharmony_ci 7698c2ecf20Sopenharmony_ci swim_motor(base, OFF); 7708c2ecf20Sopenharmony_ci 7718c2ecf20Sopenharmony_ci fs->type = HD_MEDIA; 7728c2ecf20Sopenharmony_ci fs->head_number = 2; 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci fs->ref_count = 0; 7758c2ecf20Sopenharmony_ci fs->ejected = 1; 7768c2ecf20Sopenharmony_ci 7778c2ecf20Sopenharmony_ci swd->floppy_count++; 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci return 0; 7808c2ecf20Sopenharmony_ci} 7818c2ecf20Sopenharmony_ci 7828c2ecf20Sopenharmony_cistatic const struct blk_mq_ops swim_mq_ops = { 7838c2ecf20Sopenharmony_ci .queue_rq = swim_queue_rq, 7848c2ecf20Sopenharmony_ci}; 7858c2ecf20Sopenharmony_ci 7868c2ecf20Sopenharmony_cistatic int swim_floppy_init(struct swim_priv *swd) 7878c2ecf20Sopenharmony_ci{ 7888c2ecf20Sopenharmony_ci int err; 7898c2ecf20Sopenharmony_ci int drive; 7908c2ecf20Sopenharmony_ci struct swim __iomem *base = swd->base; 7918c2ecf20Sopenharmony_ci 7928c2ecf20Sopenharmony_ci /* scan floppy drives */ 7938c2ecf20Sopenharmony_ci 7948c2ecf20Sopenharmony_ci swim_drive(base, INTERNAL_DRIVE); 7958c2ecf20Sopenharmony_ci if (swim_readbit(base, DRIVE_PRESENT) && 7968c2ecf20Sopenharmony_ci !swim_readbit(base, ONEMEG_DRIVE)) 7978c2ecf20Sopenharmony_ci swim_add_floppy(swd, INTERNAL_DRIVE); 7988c2ecf20Sopenharmony_ci swim_drive(base, EXTERNAL_DRIVE); 7998c2ecf20Sopenharmony_ci if (swim_readbit(base, DRIVE_PRESENT) && 8008c2ecf20Sopenharmony_ci !swim_readbit(base, ONEMEG_DRIVE)) 8018c2ecf20Sopenharmony_ci swim_add_floppy(swd, EXTERNAL_DRIVE); 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci /* register floppy drives */ 8048c2ecf20Sopenharmony_ci 8058c2ecf20Sopenharmony_ci err = register_blkdev(FLOPPY_MAJOR, "fd"); 8068c2ecf20Sopenharmony_ci if (err) { 8078c2ecf20Sopenharmony_ci printk(KERN_ERR "Unable to get major %d for SWIM floppy\n", 8088c2ecf20Sopenharmony_ci FLOPPY_MAJOR); 8098c2ecf20Sopenharmony_ci return -EBUSY; 8108c2ecf20Sopenharmony_ci } 8118c2ecf20Sopenharmony_ci 8128c2ecf20Sopenharmony_ci spin_lock_init(&swd->lock); 8138c2ecf20Sopenharmony_ci 8148c2ecf20Sopenharmony_ci for (drive = 0; drive < swd->floppy_count; drive++) { 8158c2ecf20Sopenharmony_ci struct request_queue *q; 8168c2ecf20Sopenharmony_ci 8178c2ecf20Sopenharmony_ci swd->unit[drive].disk = alloc_disk(1); 8188c2ecf20Sopenharmony_ci if (swd->unit[drive].disk == NULL) { 8198c2ecf20Sopenharmony_ci err = -ENOMEM; 8208c2ecf20Sopenharmony_ci goto exit_put_disks; 8218c2ecf20Sopenharmony_ci } 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci q = blk_mq_init_sq_queue(&swd->unit[drive].tag_set, &swim_mq_ops, 8248c2ecf20Sopenharmony_ci 2, BLK_MQ_F_SHOULD_MERGE); 8258c2ecf20Sopenharmony_ci if (IS_ERR(q)) { 8268c2ecf20Sopenharmony_ci err = PTR_ERR(q); 8278c2ecf20Sopenharmony_ci goto exit_put_disks; 8288c2ecf20Sopenharmony_ci } 8298c2ecf20Sopenharmony_ci 8308c2ecf20Sopenharmony_ci swd->unit[drive].disk->queue = q; 8318c2ecf20Sopenharmony_ci blk_queue_bounce_limit(swd->unit[drive].disk->queue, 8328c2ecf20Sopenharmony_ci BLK_BOUNCE_HIGH); 8338c2ecf20Sopenharmony_ci swd->unit[drive].disk->queue->queuedata = &swd->unit[drive]; 8348c2ecf20Sopenharmony_ci swd->unit[drive].swd = swd; 8358c2ecf20Sopenharmony_ci } 8368c2ecf20Sopenharmony_ci 8378c2ecf20Sopenharmony_ci for (drive = 0; drive < swd->floppy_count; drive++) { 8388c2ecf20Sopenharmony_ci swd->unit[drive].disk->flags = GENHD_FL_REMOVABLE; 8398c2ecf20Sopenharmony_ci swd->unit[drive].disk->major = FLOPPY_MAJOR; 8408c2ecf20Sopenharmony_ci swd->unit[drive].disk->first_minor = drive; 8418c2ecf20Sopenharmony_ci sprintf(swd->unit[drive].disk->disk_name, "fd%d", drive); 8428c2ecf20Sopenharmony_ci swd->unit[drive].disk->fops = &floppy_fops; 8438c2ecf20Sopenharmony_ci swd->unit[drive].disk->events = DISK_EVENT_MEDIA_CHANGE; 8448c2ecf20Sopenharmony_ci swd->unit[drive].disk->private_data = &swd->unit[drive]; 8458c2ecf20Sopenharmony_ci set_capacity(swd->unit[drive].disk, 2880); 8468c2ecf20Sopenharmony_ci add_disk(swd->unit[drive].disk); 8478c2ecf20Sopenharmony_ci } 8488c2ecf20Sopenharmony_ci 8498c2ecf20Sopenharmony_ci blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE, 8508c2ecf20Sopenharmony_ci floppy_find, NULL, swd); 8518c2ecf20Sopenharmony_ci 8528c2ecf20Sopenharmony_ci return 0; 8538c2ecf20Sopenharmony_ci 8548c2ecf20Sopenharmony_ciexit_put_disks: 8558c2ecf20Sopenharmony_ci unregister_blkdev(FLOPPY_MAJOR, "fd"); 8568c2ecf20Sopenharmony_ci do { 8578c2ecf20Sopenharmony_ci struct gendisk *disk = swd->unit[drive].disk; 8588c2ecf20Sopenharmony_ci 8598c2ecf20Sopenharmony_ci if (disk) { 8608c2ecf20Sopenharmony_ci if (disk->queue) { 8618c2ecf20Sopenharmony_ci blk_cleanup_queue(disk->queue); 8628c2ecf20Sopenharmony_ci disk->queue = NULL; 8638c2ecf20Sopenharmony_ci } 8648c2ecf20Sopenharmony_ci blk_mq_free_tag_set(&swd->unit[drive].tag_set); 8658c2ecf20Sopenharmony_ci put_disk(disk); 8668c2ecf20Sopenharmony_ci } 8678c2ecf20Sopenharmony_ci } while (drive--); 8688c2ecf20Sopenharmony_ci return err; 8698c2ecf20Sopenharmony_ci} 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_cistatic int swim_probe(struct platform_device *dev) 8728c2ecf20Sopenharmony_ci{ 8738c2ecf20Sopenharmony_ci struct resource *res; 8748c2ecf20Sopenharmony_ci struct swim __iomem *swim_base; 8758c2ecf20Sopenharmony_ci struct swim_priv *swd; 8768c2ecf20Sopenharmony_ci int ret; 8778c2ecf20Sopenharmony_ci 8788c2ecf20Sopenharmony_ci res = platform_get_resource(dev, IORESOURCE_MEM, 0); 8798c2ecf20Sopenharmony_ci if (!res) { 8808c2ecf20Sopenharmony_ci ret = -ENODEV; 8818c2ecf20Sopenharmony_ci goto out; 8828c2ecf20Sopenharmony_ci } 8838c2ecf20Sopenharmony_ci 8848c2ecf20Sopenharmony_ci if (!request_mem_region(res->start, resource_size(res), CARDNAME)) { 8858c2ecf20Sopenharmony_ci ret = -EBUSY; 8868c2ecf20Sopenharmony_ci goto out; 8878c2ecf20Sopenharmony_ci } 8888c2ecf20Sopenharmony_ci 8898c2ecf20Sopenharmony_ci swim_base = (struct swim __iomem *)res->start; 8908c2ecf20Sopenharmony_ci if (!swim_base) { 8918c2ecf20Sopenharmony_ci ret = -ENOMEM; 8928c2ecf20Sopenharmony_ci goto out_release_io; 8938c2ecf20Sopenharmony_ci } 8948c2ecf20Sopenharmony_ci 8958c2ecf20Sopenharmony_ci /* probe device */ 8968c2ecf20Sopenharmony_ci 8978c2ecf20Sopenharmony_ci set_swim_mode(swim_base, 1); 8988c2ecf20Sopenharmony_ci if (!get_swim_mode(swim_base)) { 8998c2ecf20Sopenharmony_ci printk(KERN_INFO "SWIM device not found !\n"); 9008c2ecf20Sopenharmony_ci ret = -ENODEV; 9018c2ecf20Sopenharmony_ci goto out_release_io; 9028c2ecf20Sopenharmony_ci } 9038c2ecf20Sopenharmony_ci 9048c2ecf20Sopenharmony_ci /* set platform driver data */ 9058c2ecf20Sopenharmony_ci 9068c2ecf20Sopenharmony_ci swd = kzalloc(sizeof(struct swim_priv), GFP_KERNEL); 9078c2ecf20Sopenharmony_ci if (!swd) { 9088c2ecf20Sopenharmony_ci ret = -ENOMEM; 9098c2ecf20Sopenharmony_ci goto out_release_io; 9108c2ecf20Sopenharmony_ci } 9118c2ecf20Sopenharmony_ci platform_set_drvdata(dev, swd); 9128c2ecf20Sopenharmony_ci 9138c2ecf20Sopenharmony_ci swd->base = swim_base; 9148c2ecf20Sopenharmony_ci 9158c2ecf20Sopenharmony_ci ret = swim_floppy_init(swd); 9168c2ecf20Sopenharmony_ci if (ret) 9178c2ecf20Sopenharmony_ci goto out_kfree; 9188c2ecf20Sopenharmony_ci 9198c2ecf20Sopenharmony_ci return 0; 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ciout_kfree: 9228c2ecf20Sopenharmony_ci kfree(swd); 9238c2ecf20Sopenharmony_ciout_release_io: 9248c2ecf20Sopenharmony_ci release_mem_region(res->start, resource_size(res)); 9258c2ecf20Sopenharmony_ciout: 9268c2ecf20Sopenharmony_ci return ret; 9278c2ecf20Sopenharmony_ci} 9288c2ecf20Sopenharmony_ci 9298c2ecf20Sopenharmony_cistatic int swim_remove(struct platform_device *dev) 9308c2ecf20Sopenharmony_ci{ 9318c2ecf20Sopenharmony_ci struct swim_priv *swd = platform_get_drvdata(dev); 9328c2ecf20Sopenharmony_ci int drive; 9338c2ecf20Sopenharmony_ci struct resource *res; 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256); 9368c2ecf20Sopenharmony_ci 9378c2ecf20Sopenharmony_ci for (drive = 0; drive < swd->floppy_count; drive++) { 9388c2ecf20Sopenharmony_ci del_gendisk(swd->unit[drive].disk); 9398c2ecf20Sopenharmony_ci blk_cleanup_queue(swd->unit[drive].disk->queue); 9408c2ecf20Sopenharmony_ci blk_mq_free_tag_set(&swd->unit[drive].tag_set); 9418c2ecf20Sopenharmony_ci put_disk(swd->unit[drive].disk); 9428c2ecf20Sopenharmony_ci } 9438c2ecf20Sopenharmony_ci 9448c2ecf20Sopenharmony_ci unregister_blkdev(FLOPPY_MAJOR, "fd"); 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci /* eject floppies */ 9478c2ecf20Sopenharmony_ci 9488c2ecf20Sopenharmony_ci for (drive = 0; drive < swd->floppy_count; drive++) 9498c2ecf20Sopenharmony_ci floppy_eject(&swd->unit[drive]); 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_ci res = platform_get_resource(dev, IORESOURCE_MEM, 0); 9528c2ecf20Sopenharmony_ci if (res) 9538c2ecf20Sopenharmony_ci release_mem_region(res->start, resource_size(res)); 9548c2ecf20Sopenharmony_ci 9558c2ecf20Sopenharmony_ci kfree(swd); 9568c2ecf20Sopenharmony_ci 9578c2ecf20Sopenharmony_ci return 0; 9588c2ecf20Sopenharmony_ci} 9598c2ecf20Sopenharmony_ci 9608c2ecf20Sopenharmony_cistatic struct platform_driver swim_driver = { 9618c2ecf20Sopenharmony_ci .probe = swim_probe, 9628c2ecf20Sopenharmony_ci .remove = swim_remove, 9638c2ecf20Sopenharmony_ci .driver = { 9648c2ecf20Sopenharmony_ci .name = CARDNAME, 9658c2ecf20Sopenharmony_ci }, 9668c2ecf20Sopenharmony_ci}; 9678c2ecf20Sopenharmony_ci 9688c2ecf20Sopenharmony_cistatic int __init swim_init(void) 9698c2ecf20Sopenharmony_ci{ 9708c2ecf20Sopenharmony_ci printk(KERN_INFO "SWIM floppy driver %s\n", DRIVER_VERSION); 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci return platform_driver_register(&swim_driver); 9738c2ecf20Sopenharmony_ci} 9748c2ecf20Sopenharmony_cimodule_init(swim_init); 9758c2ecf20Sopenharmony_ci 9768c2ecf20Sopenharmony_cistatic void __exit swim_exit(void) 9778c2ecf20Sopenharmony_ci{ 9788c2ecf20Sopenharmony_ci platform_driver_unregister(&swim_driver); 9798c2ecf20Sopenharmony_ci} 9808c2ecf20Sopenharmony_cimodule_exit(swim_exit); 9818c2ecf20Sopenharmony_ci 9828c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Driver for SWIM floppy controller"); 9838c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 9848c2ecf20Sopenharmony_ciMODULE_AUTHOR("Laurent Vivier <laurent@lvivier.info>"); 9858c2ecf20Sopenharmony_ciMODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); 986