xref: /linux/drivers/memstick/core/ms_block.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1*d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
20ab30494SMaxim Levitsky /*
30ab30494SMaxim Levitsky  *  ms_block.h - Sony MemoryStick (legacy) storage support
40ab30494SMaxim Levitsky 
50ab30494SMaxim Levitsky  *  Copyright (C) 2013 Maxim Levitsky <maximlevitsky@gmail.com>
60ab30494SMaxim Levitsky  *
70ab30494SMaxim Levitsky  * Minor portions of the driver are copied from mspro_block.c which is
80ab30494SMaxim Levitsky  * Copyright (C) 2007 Alex Dubov <oakad@yahoo.com>
90ab30494SMaxim Levitsky  *
100ab30494SMaxim Levitsky  * Also ms structures were copied from old broken driver by same author
110ab30494SMaxim Levitsky  * These probably come from MS spec
120ab30494SMaxim Levitsky  */
130ab30494SMaxim Levitsky 
140ab30494SMaxim Levitsky #ifndef MS_BLOCK_NEW_H
150ab30494SMaxim Levitsky #define MS_BLOCK_NEW_H
160ab30494SMaxim Levitsky 
170ab30494SMaxim Levitsky #define MS_BLOCK_MAX_SEGS      32
180ab30494SMaxim Levitsky #define MS_BLOCK_MAX_PAGES     ((2 << 16) - 1)
190ab30494SMaxim Levitsky 
200ab30494SMaxim Levitsky #define MS_BLOCK_MAX_BOOT_ADDR 0x000c
210ab30494SMaxim Levitsky #define MS_BLOCK_BOOT_ID       0x0001
220ab30494SMaxim Levitsky #define MS_BLOCK_INVALID       0xffff
230ab30494SMaxim Levitsky #define MS_MAX_ZONES           16
240ab30494SMaxim Levitsky #define MS_BLOCKS_IN_ZONE      512
250ab30494SMaxim Levitsky 
260ab30494SMaxim Levitsky #define MS_BLOCK_MAP_LINE_SZ   16
270ab30494SMaxim Levitsky #define MS_BLOCK_PART_SHIFT    3
280ab30494SMaxim Levitsky 
290ab30494SMaxim Levitsky 
300ab30494SMaxim Levitsky #define MEMSTICK_UNCORR_ERROR (MEMSTICK_STATUS1_UCFG | \
310ab30494SMaxim Levitsky 		MEMSTICK_STATUS1_UCEX | MEMSTICK_STATUS1_UCDT)
320ab30494SMaxim Levitsky 
330ab30494SMaxim Levitsky #define MEMSTICK_CORR_ERROR (MEMSTICK_STATUS1_FGER | MEMSTICK_STATUS1_EXER | \
340ab30494SMaxim Levitsky 	MEMSTICK_STATUS1_DTER)
350ab30494SMaxim Levitsky 
360ab30494SMaxim Levitsky #define MEMSTICK_INT_ERROR (MEMSTICK_INT_CMDNAK | MEMSTICK_INT_ERR)
370ab30494SMaxim Levitsky 
380ab30494SMaxim Levitsky #define MEMSTICK_OVERWRITE_FLAG_NORMAL \
390ab30494SMaxim Levitsky 	(MEMSTICK_OVERWRITE_PGST1 | \
400ab30494SMaxim Levitsky 	MEMSTICK_OVERWRITE_PGST0  | \
410ab30494SMaxim Levitsky 	MEMSTICK_OVERWRITE_BKST)
420ab30494SMaxim Levitsky 
430ab30494SMaxim Levitsky #define MEMSTICK_OV_PG_NORMAL \
440ab30494SMaxim Levitsky 	(MEMSTICK_OVERWRITE_PGST1 | MEMSTICK_OVERWRITE_PGST0)
450ab30494SMaxim Levitsky 
466951c585SColin Ian King #define MEMSTICK_MANAGEMENT_FLAG_NORMAL \
470ab30494SMaxim Levitsky 	(MEMSTICK_MANAGEMENT_SYSFLG |  \
480ab30494SMaxim Levitsky 	MEMSTICK_MANAGEMENT_SCMS1   |  \
490ab30494SMaxim Levitsky 	MEMSTICK_MANAGEMENT_SCMS0)     \
500ab30494SMaxim Levitsky 
510ab30494SMaxim Levitsky struct ms_boot_header {
520ab30494SMaxim Levitsky 	unsigned short block_id;
530ab30494SMaxim Levitsky 	unsigned short format_reserved;
540ab30494SMaxim Levitsky 	unsigned char  reserved0[184];
550ab30494SMaxim Levitsky 	unsigned char  data_entry;
560ab30494SMaxim Levitsky 	unsigned char  reserved1[179];
570ab30494SMaxim Levitsky } __packed;
580ab30494SMaxim Levitsky 
590ab30494SMaxim Levitsky 
600ab30494SMaxim Levitsky struct ms_system_item {
610ab30494SMaxim Levitsky 	unsigned int  start_addr;
620ab30494SMaxim Levitsky 	unsigned int  data_size;
630ab30494SMaxim Levitsky 	unsigned char data_type_id;
640ab30494SMaxim Levitsky 	unsigned char reserved[3];
650ab30494SMaxim Levitsky } __packed;
660ab30494SMaxim Levitsky 
670ab30494SMaxim Levitsky struct ms_system_entry {
680ab30494SMaxim Levitsky 	struct ms_system_item disabled_block;
690ab30494SMaxim Levitsky 	struct ms_system_item cis_idi;
700ab30494SMaxim Levitsky 	unsigned char         reserved[24];
710ab30494SMaxim Levitsky } __packed;
720ab30494SMaxim Levitsky 
730ab30494SMaxim Levitsky struct ms_boot_attr_info {
740ab30494SMaxim Levitsky 	unsigned char      memorystick_class;
750ab30494SMaxim Levitsky 	unsigned char      format_unique_value1;
760ab30494SMaxim Levitsky 	unsigned short     block_size;
770ab30494SMaxim Levitsky 	unsigned short     number_of_blocks;
780ab30494SMaxim Levitsky 	unsigned short     number_of_effective_blocks;
790ab30494SMaxim Levitsky 	unsigned short     page_size;
800ab30494SMaxim Levitsky 	unsigned char      extra_data_size;
810ab30494SMaxim Levitsky 	unsigned char      format_unique_value2;
820ab30494SMaxim Levitsky 	unsigned char      assembly_time[8];
830ab30494SMaxim Levitsky 	unsigned char      format_unique_value3;
840ab30494SMaxim Levitsky 	unsigned char      serial_number[3];
850ab30494SMaxim Levitsky 	unsigned char      assembly_manufacturer_code;
860ab30494SMaxim Levitsky 	unsigned char      assembly_model_code[3];
870ab30494SMaxim Levitsky 	unsigned short     memory_manufacturer_code;
880ab30494SMaxim Levitsky 	unsigned short     memory_device_code;
890ab30494SMaxim Levitsky 	unsigned short     implemented_capacity;
900ab30494SMaxim Levitsky 	unsigned char      format_unique_value4[2];
910ab30494SMaxim Levitsky 	unsigned char      vcc;
920ab30494SMaxim Levitsky 	unsigned char      vpp;
930ab30494SMaxim Levitsky 	unsigned short     controller_number;
940ab30494SMaxim Levitsky 	unsigned short     controller_function;
950ab30494SMaxim Levitsky 	unsigned char      reserved0[9];
960ab30494SMaxim Levitsky 	unsigned char      transfer_supporting;
970ab30494SMaxim Levitsky 	unsigned short     format_unique_value5;
980ab30494SMaxim Levitsky 	unsigned char      format_type;
990ab30494SMaxim Levitsky 	unsigned char      memorystick_application;
1000ab30494SMaxim Levitsky 	unsigned char      device_type;
1010ab30494SMaxim Levitsky 	unsigned char      reserved1[22];
1020ab30494SMaxim Levitsky 	unsigned char      format_uniqure_value6[2];
1030ab30494SMaxim Levitsky 	unsigned char      reserved2[15];
1040ab30494SMaxim Levitsky } __packed;
1050ab30494SMaxim Levitsky 
1060ab30494SMaxim Levitsky struct ms_cis_idi {
1070ab30494SMaxim Levitsky 	unsigned short general_config;
1080ab30494SMaxim Levitsky 	unsigned short logical_cylinders;
1090ab30494SMaxim Levitsky 	unsigned short reserved0;
1100ab30494SMaxim Levitsky 	unsigned short logical_heads;
1110ab30494SMaxim Levitsky 	unsigned short track_size;
1120ab30494SMaxim Levitsky 	unsigned short page_size;
1130ab30494SMaxim Levitsky 	unsigned short pages_per_track;
1140ab30494SMaxim Levitsky 	unsigned short msw;
1150ab30494SMaxim Levitsky 	unsigned short lsw;
1160ab30494SMaxim Levitsky 	unsigned short reserved1;
1170ab30494SMaxim Levitsky 	unsigned char  serial_number[20];
1180ab30494SMaxim Levitsky 	unsigned short buffer_type;
1190ab30494SMaxim Levitsky 	unsigned short buffer_size_increments;
1200ab30494SMaxim Levitsky 	unsigned short long_command_ecc;
1210ab30494SMaxim Levitsky 	unsigned char  firmware_version[28];
1220ab30494SMaxim Levitsky 	unsigned char  model_name[18];
1230ab30494SMaxim Levitsky 	unsigned short reserved2[5];
1240ab30494SMaxim Levitsky 	unsigned short pio_mode_number;
1250ab30494SMaxim Levitsky 	unsigned short dma_mode_number;
1260ab30494SMaxim Levitsky 	unsigned short field_validity;
1270ab30494SMaxim Levitsky 	unsigned short current_logical_cylinders;
1280ab30494SMaxim Levitsky 	unsigned short current_logical_heads;
1290ab30494SMaxim Levitsky 	unsigned short current_pages_per_track;
1300ab30494SMaxim Levitsky 	unsigned int   current_page_capacity;
1310ab30494SMaxim Levitsky 	unsigned short mutiple_page_setting;
1320ab30494SMaxim Levitsky 	unsigned int   addressable_pages;
1330ab30494SMaxim Levitsky 	unsigned short single_word_dma;
1340ab30494SMaxim Levitsky 	unsigned short multi_word_dma;
1350ab30494SMaxim Levitsky 	unsigned char  reserved3[128];
1360ab30494SMaxim Levitsky } __packed;
1370ab30494SMaxim Levitsky 
1380ab30494SMaxim Levitsky 
1390ab30494SMaxim Levitsky struct ms_boot_page {
1400ab30494SMaxim Levitsky 	struct ms_boot_header    header;
1410ab30494SMaxim Levitsky 	struct ms_system_entry   entry;
1420ab30494SMaxim Levitsky 	struct ms_boot_attr_info attr;
1430ab30494SMaxim Levitsky } __packed;
1440ab30494SMaxim Levitsky 
1450ab30494SMaxim Levitsky struct msb_data {
1460ab30494SMaxim Levitsky 	struct memstick_dev		*card;
1470ab30494SMaxim Levitsky 	struct gendisk			*disk;
1480ab30494SMaxim Levitsky 	struct request_queue		*queue;
1490ab30494SMaxim Levitsky 	spinlock_t			q_lock;
150db1142a8SJens Axboe 	struct blk_mq_tag_set		tag_set;
1510ab30494SMaxim Levitsky 	struct hd_geometry		geometry;
1520ab30494SMaxim Levitsky 	struct attribute_group		attr_group;
1530ab30494SMaxim Levitsky 	struct request			*req;
1540ab30494SMaxim Levitsky 	int				caps;
1550ab30494SMaxim Levitsky 	int				disk_id;
1560ab30494SMaxim Levitsky 
1570ab30494SMaxim Levitsky 	/* IO */
1580ab30494SMaxim Levitsky 	struct workqueue_struct		*io_queue;
1590ab30494SMaxim Levitsky 	bool				io_queue_stopped;
1600ab30494SMaxim Levitsky 	struct work_struct		io_work;
1610ab30494SMaxim Levitsky 	bool				card_dead;
1620ab30494SMaxim Levitsky 
1630ab30494SMaxim Levitsky 	/* Media properties */
1640ab30494SMaxim Levitsky 	struct ms_boot_page		*boot_page;
1650ab30494SMaxim Levitsky 	u16				boot_block_locations[2];
1660ab30494SMaxim Levitsky 	int				boot_block_count;
1670ab30494SMaxim Levitsky 
1680ab30494SMaxim Levitsky 	bool				read_only;
1690ab30494SMaxim Levitsky 	unsigned short			page_size;
1700ab30494SMaxim Levitsky 	int				block_size;
1710ab30494SMaxim Levitsky 	int				pages_in_block;
1720ab30494SMaxim Levitsky 	int				zone_count;
1730ab30494SMaxim Levitsky 	int				block_count;
1740ab30494SMaxim Levitsky 	int				logical_block_count;
1750ab30494SMaxim Levitsky 
1760ab30494SMaxim Levitsky 	/* FTL tables */
1770ab30494SMaxim Levitsky 	unsigned long			*used_blocks_bitmap;
1780ab30494SMaxim Levitsky 	unsigned long			*erased_blocks_bitmap;
1790ab30494SMaxim Levitsky 	u16				*lba_to_pba_table;
1800ab30494SMaxim Levitsky 	int				free_block_count[MS_MAX_ZONES];
1810ab30494SMaxim Levitsky 	bool				ftl_initialized;
1820ab30494SMaxim Levitsky 
1830ab30494SMaxim Levitsky 	/* Cache */
1840ab30494SMaxim Levitsky 	unsigned char			*cache;
1850ab30494SMaxim Levitsky 	unsigned long			valid_cache_bitmap;
1860ab30494SMaxim Levitsky 	int				cache_block_lba;
1870ab30494SMaxim Levitsky 	bool				need_flush_cache;
1880ab30494SMaxim Levitsky 	struct timer_list		cache_flush_timer;
1890ab30494SMaxim Levitsky 
1900ab30494SMaxim Levitsky 	/* Preallocated buffers */
1910ab30494SMaxim Levitsky 	unsigned char			*block_buffer;
1920ab30494SMaxim Levitsky 	struct scatterlist		prealloc_sg[MS_BLOCK_MAX_SEGS+1];
1930ab30494SMaxim Levitsky 
1940ab30494SMaxim Levitsky 
1950ab30494SMaxim Levitsky 	/* handler's local data */
1960ab30494SMaxim Levitsky 	struct ms_register_addr		reg_addr;
1970ab30494SMaxim Levitsky 	bool				addr_valid;
1980ab30494SMaxim Levitsky 
1990ab30494SMaxim Levitsky 	u8				command_value;
2000ab30494SMaxim Levitsky 	bool				command_need_oob;
2010ab30494SMaxim Levitsky 	struct scatterlist		*current_sg;
2020ab30494SMaxim Levitsky 	int				current_sg_offset;
2030ab30494SMaxim Levitsky 
2040ab30494SMaxim Levitsky 	struct ms_register		regs;
2050ab30494SMaxim Levitsky 	int				current_page;
2060ab30494SMaxim Levitsky 
2070ab30494SMaxim Levitsky 	int				state;
2080ab30494SMaxim Levitsky 	int				exit_error;
2090ab30494SMaxim Levitsky 	bool				int_polling;
2100ab30494SMaxim Levitsky 	unsigned long			int_timeout;
2110ab30494SMaxim Levitsky 
2120ab30494SMaxim Levitsky };
2130ab30494SMaxim Levitsky 
2140ab30494SMaxim Levitsky enum msb_readpage_states {
2150ab30494SMaxim Levitsky 	MSB_RP_SEND_BLOCK_ADDRESS = 0,
2160ab30494SMaxim Levitsky 	MSB_RP_SEND_READ_COMMAND,
2170ab30494SMaxim Levitsky 
2180ab30494SMaxim Levitsky 	MSB_RP_SEND_INT_REQ,
2190ab30494SMaxim Levitsky 	MSB_RP_RECEIVE_INT_REQ_RESULT,
2200ab30494SMaxim Levitsky 
2210ab30494SMaxim Levitsky 	MSB_RP_SEND_READ_STATUS_REG,
222a0dce7f0SAndrew Morton 	MSB_RP_RECEIVE_STATUS_REG,
2230ab30494SMaxim Levitsky 
2240ab30494SMaxim Levitsky 	MSB_RP_SEND_OOB_READ,
2250ab30494SMaxim Levitsky 	MSB_RP_RECEIVE_OOB_READ,
2260ab30494SMaxim Levitsky 
2270ab30494SMaxim Levitsky 	MSB_RP_SEND_READ_DATA,
2280ab30494SMaxim Levitsky 	MSB_RP_RECEIVE_READ_DATA,
2290ab30494SMaxim Levitsky };
2300ab30494SMaxim Levitsky 
2310ab30494SMaxim Levitsky enum msb_write_block_states {
2320ab30494SMaxim Levitsky 	MSB_WB_SEND_WRITE_PARAMS = 0,
2330ab30494SMaxim Levitsky 	MSB_WB_SEND_WRITE_OOB,
2340ab30494SMaxim Levitsky 	MSB_WB_SEND_WRITE_COMMAND,
2350ab30494SMaxim Levitsky 
2360ab30494SMaxim Levitsky 	MSB_WB_SEND_INT_REQ,
2370ab30494SMaxim Levitsky 	MSB_WB_RECEIVE_INT_REQ,
2380ab30494SMaxim Levitsky 
2390ab30494SMaxim Levitsky 	MSB_WB_SEND_WRITE_DATA,
2400ab30494SMaxim Levitsky 	MSB_WB_RECEIVE_WRITE_CONFIRMATION,
2410ab30494SMaxim Levitsky };
2420ab30494SMaxim Levitsky 
2430ab30494SMaxim Levitsky enum msb_send_command_states {
2440ab30494SMaxim Levitsky 	MSB_SC_SEND_WRITE_PARAMS,
2450ab30494SMaxim Levitsky 	MSB_SC_SEND_WRITE_OOB,
2460ab30494SMaxim Levitsky 	MSB_SC_SEND_COMMAND,
2470ab30494SMaxim Levitsky 
2480ab30494SMaxim Levitsky 	MSB_SC_SEND_INT_REQ,
2490ab30494SMaxim Levitsky 	MSB_SC_RECEIVE_INT_REQ,
2500ab30494SMaxim Levitsky 
2510ab30494SMaxim Levitsky };
2520ab30494SMaxim Levitsky 
2530ab30494SMaxim Levitsky enum msb_reset_states {
2540ab30494SMaxim Levitsky 	MSB_RS_SEND,
2550ab30494SMaxim Levitsky 	MSB_RS_CONFIRM,
2560ab30494SMaxim Levitsky };
2570ab30494SMaxim Levitsky 
2580ab30494SMaxim Levitsky enum msb_par_switch_states {
2590ab30494SMaxim Levitsky 	MSB_PS_SEND_SWITCH_COMMAND,
2600ab30494SMaxim Levitsky 	MSB_PS_SWICH_HOST,
2610ab30494SMaxim Levitsky 	MSB_PS_CONFIRM,
2620ab30494SMaxim Levitsky };
2630ab30494SMaxim Levitsky 
2640ab30494SMaxim Levitsky struct chs_entry {
2650ab30494SMaxim Levitsky 	unsigned long size;
2660ab30494SMaxim Levitsky 	unsigned char sec;
2670ab30494SMaxim Levitsky 	unsigned short cyl;
2680ab30494SMaxim Levitsky 	unsigned char head;
2690ab30494SMaxim Levitsky };
2700ab30494SMaxim Levitsky 
2710ab30494SMaxim Levitsky static int msb_reset(struct msb_data *msb, bool full);
2720ab30494SMaxim Levitsky 
2730ab30494SMaxim Levitsky static int h_msb_default_bad(struct memstick_dev *card,
2740ab30494SMaxim Levitsky 						struct memstick_request **mrq);
2750ab30494SMaxim Levitsky 
2760ab30494SMaxim Levitsky #define __dbg(level, format, ...) \
2770ab30494SMaxim Levitsky 	do { \
2780ab30494SMaxim Levitsky 		if (debug >= level) \
2790ab30494SMaxim Levitsky 			pr_err(format "\n", ## __VA_ARGS__); \
2800ab30494SMaxim Levitsky 	} while (0)
2810ab30494SMaxim Levitsky 
2820ab30494SMaxim Levitsky 
2830ab30494SMaxim Levitsky #define dbg(format, ...)		__dbg(1, format, ## __VA_ARGS__)
2840ab30494SMaxim Levitsky #define dbg_verbose(format, ...)	__dbg(2, format, ## __VA_ARGS__)
2850ab30494SMaxim Levitsky 
2860ab30494SMaxim Levitsky #endif
287