Path: blob/master/drivers/char/xilinx_hwicap/fifo_icap.c
26285 views
/*****************************************************************************1*2* Author: Xilinx, Inc.3*4* This program is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License as published by the6* Free Software Foundation; either version 2 of the License, or (at your7* option) any later version.8*9* XILINX IS PROVIDING THIS DESIGN, CODE, OR INFORMATION "AS IS"10* AS A COURTESY TO YOU, SOLELY FOR USE IN DEVELOPING PROGRAMS AND11* SOLUTIONS FOR XILINX DEVICES. BY PROVIDING THIS DESIGN, CODE,12* OR INFORMATION AS ONE POSSIBLE IMPLEMENTATION OF THIS FEATURE,13* APPLICATION OR STANDARD, XILINX IS MAKING NO REPRESENTATION14* THAT THIS IMPLEMENTATION IS FREE FROM ANY CLAIMS OF INFRINGEMENT,15* AND YOU ARE RESPONSIBLE FOR OBTAINING ANY RIGHTS YOU MAY REQUIRE16* FOR YOUR IMPLEMENTATION. XILINX EXPRESSLY DISCLAIMS ANY17* WARRANTY WHATSOEVER WITH RESPECT TO THE ADEQUACY OF THE18* IMPLEMENTATION, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OR19* REPRESENTATIONS THAT THIS IMPLEMENTATION IS FREE FROM CLAIMS OF20* INFRINGEMENT, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS21* FOR A PARTICULAR PURPOSE.22*23* (c) Copyright 2007-2008 Xilinx Inc.24* All rights reserved.25*26* You should have received a copy of the GNU General Public License along27* with this program; if not, write to the Free Software Foundation, Inc.,28* 675 Mass Ave, Cambridge, MA 02139, USA.29*30*****************************************************************************/3132#include "fifo_icap.h"3334/* Register offsets for the XHwIcap device. */35#define XHI_GIER_OFFSET 0x1C /* Device Global Interrupt Enable Reg */36#define XHI_IPISR_OFFSET 0x20 /* Interrupt Status Register */37#define XHI_IPIER_OFFSET 0x28 /* Interrupt Enable Register */38#define XHI_WF_OFFSET 0x100 /* Write FIFO */39#define XHI_RF_OFFSET 0x104 /* Read FIFO */40#define XHI_SZ_OFFSET 0x108 /* Size Register */41#define XHI_CR_OFFSET 0x10C /* Control Register */42#define XHI_SR_OFFSET 0x110 /* Status Register */43#define XHI_WFV_OFFSET 0x114 /* Write FIFO Vacancy Register */44#define XHI_RFO_OFFSET 0x118 /* Read FIFO Occupancy Register */4546/* Device Global Interrupt Enable Register (GIER) bit definitions */4748#define XHI_GIER_GIE_MASK 0x80000000 /* Global Interrupt enable Mask */4950/**51* HwIcap Device Interrupt Status/Enable Registers52*53* Interrupt Status Register (IPISR) : This register holds the54* interrupt status flags for the device. These bits are toggle on55* write.56*57* Interrupt Enable Register (IPIER) : This register is used to enable58* interrupt sources for the device.59* Writing a '1' to a bit enables the corresponding interrupt.60* Writing a '0' to a bit disables the corresponding interrupt.61*62* IPISR/IPIER registers have the same bit definitions and are only defined63* once.64*/65#define XHI_IPIXR_RFULL_MASK 0x00000008 /* Read FIFO Full */66#define XHI_IPIXR_WEMPTY_MASK 0x00000004 /* Write FIFO Empty */67#define XHI_IPIXR_RDP_MASK 0x00000002 /* Read FIFO half full */68#define XHI_IPIXR_WRP_MASK 0x00000001 /* Write FIFO half full */69#define XHI_IPIXR_ALL_MASK 0x0000000F /* Mask of all interrupts */7071/* Control Register (CR) */72#define XHI_CR_SW_RESET_MASK 0x00000008 /* SW Reset Mask */73#define XHI_CR_FIFO_CLR_MASK 0x00000004 /* FIFO Clear Mask */74#define XHI_CR_READ_MASK 0x00000002 /* Read from ICAP to FIFO */75#define XHI_CR_WRITE_MASK 0x00000001 /* Write from FIFO to ICAP */767778#define XHI_WFO_MAX_VACANCY 1024 /* Max Write FIFO Vacancy, in words */79#define XHI_RFO_MAX_OCCUPANCY 256 /* Max Read FIFO Occupancy, in words */80/* The maximum amount we can request from fifo_icap_get_configuration81at once, in bytes. */82#define XHI_MAX_READ_TRANSACTION_WORDS 0xFFF838485/**86* fifo_icap_fifo_write - Write data to the write FIFO.87* @drvdata: a pointer to the drvdata.88* @data: the 32-bit value to be written to the FIFO.89*90* This function will silently fail if the fifo is full.91**/92static inline void fifo_icap_fifo_write(struct hwicap_drvdata *drvdata,93u32 data)94{95dev_dbg(drvdata->dev, "fifo_write: %x\n", data);96out_be32(drvdata->base_address + XHI_WF_OFFSET, data);97}9899/**100* fifo_icap_fifo_read - Read data from the Read FIFO.101* @drvdata: a pointer to the drvdata.102*103* This function will silently fail if the fifo is empty.104**/105static inline u32 fifo_icap_fifo_read(struct hwicap_drvdata *drvdata)106{107u32 data = in_be32(drvdata->base_address + XHI_RF_OFFSET);108dev_dbg(drvdata->dev, "fifo_read: %x\n", data);109return data;110}111112/**113* fifo_icap_set_read_size - Set the size register.114* @drvdata: a pointer to the drvdata.115* @data: the size of the following read transaction, in words.116**/117static inline void fifo_icap_set_read_size(struct hwicap_drvdata *drvdata,118u32 data)119{120out_be32(drvdata->base_address + XHI_SZ_OFFSET, data);121}122123/**124* fifo_icap_start_config - Initiate a configuration (write) to the device.125* @drvdata: a pointer to the drvdata.126**/127static inline void fifo_icap_start_config(struct hwicap_drvdata *drvdata)128{129out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_WRITE_MASK);130dev_dbg(drvdata->dev, "configuration started\n");131}132133/**134* fifo_icap_start_readback - Initiate a readback from the device.135* @drvdata: a pointer to the drvdata.136**/137static inline void fifo_icap_start_readback(struct hwicap_drvdata *drvdata)138{139out_be32(drvdata->base_address + XHI_CR_OFFSET, XHI_CR_READ_MASK);140dev_dbg(drvdata->dev, "readback started\n");141}142143/**144* fifo_icap_get_status - Get the contents of the status register.145* @drvdata: a pointer to the drvdata.146*147* The status register contains the ICAP status and the done bit.148*149* D8 - cfgerr150* D7 - dalign151* D6 - rip152* D5 - in_abort_l153* D4 - Always 1154* D3 - Always 1155* D2 - Always 1156* D1 - Always 1157* D0 - Done bit158**/159u32 fifo_icap_get_status(struct hwicap_drvdata *drvdata)160{161u32 status = in_be32(drvdata->base_address + XHI_SR_OFFSET);162dev_dbg(drvdata->dev, "Getting status = %x\n", status);163return status;164}165166/**167* fifo_icap_busy - Return true if the ICAP is still processing a transaction.168* @drvdata: a pointer to the drvdata.169**/170static inline u32 fifo_icap_busy(struct hwicap_drvdata *drvdata)171{172u32 status = in_be32(drvdata->base_address + XHI_SR_OFFSET);173return (status & XHI_SR_DONE_MASK) ? 0 : 1;174}175176/**177* fifo_icap_write_fifo_vacancy - Query the write fifo available space.178* @drvdata: a pointer to the drvdata.179*180* Return the number of words that can be safely pushed into the write fifo.181**/182static inline u32 fifo_icap_write_fifo_vacancy(183struct hwicap_drvdata *drvdata)184{185return in_be32(drvdata->base_address + XHI_WFV_OFFSET);186}187188/**189* fifo_icap_read_fifo_occupancy - Query the read fifo available data.190* @drvdata: a pointer to the drvdata.191*192* Return the number of words that can be safely read from the read fifo.193**/194static inline u32 fifo_icap_read_fifo_occupancy(195struct hwicap_drvdata *drvdata)196{197return in_be32(drvdata->base_address + XHI_RFO_OFFSET);198}199200/**201* fifo_icap_set_configuration - Send configuration data to the ICAP.202* @drvdata: a pointer to the drvdata.203* @frame_buffer: a pointer to the data to be written to the204* ICAP device.205* @num_words: the number of words (32 bit) to write to the ICAP206* device.207208* This function writes the given user data to the Write FIFO in209* polled mode and starts the transfer of the data to210* the ICAP device.211**/212int fifo_icap_set_configuration(struct hwicap_drvdata *drvdata,213u32 *frame_buffer, u32 num_words)214{215216u32 write_fifo_vacancy = 0;217u32 retries = 0;218u32 remaining_words;219220dev_dbg(drvdata->dev, "fifo_set_configuration\n");221222/*223* Check if the ICAP device is Busy with the last Read/Write224*/225if (fifo_icap_busy(drvdata))226return -EBUSY;227228/*229* Set up the buffer pointer and the words to be transferred.230*/231remaining_words = num_words;232233while (remaining_words > 0) {234/*235* Wait until we have some data in the fifo.236*/237while (write_fifo_vacancy == 0) {238write_fifo_vacancy =239fifo_icap_write_fifo_vacancy(drvdata);240retries++;241if (retries > XHI_MAX_RETRIES)242return -EIO;243}244245/*246* Write data into the Write FIFO.247*/248while ((write_fifo_vacancy != 0) &&249(remaining_words > 0)) {250fifo_icap_fifo_write(drvdata, *frame_buffer);251252remaining_words--;253write_fifo_vacancy--;254frame_buffer++;255}256/* Start pushing whatever is in the FIFO into the ICAP. */257fifo_icap_start_config(drvdata);258}259260/* Wait until the write has finished. */261while (fifo_icap_busy(drvdata)) {262retries++;263if (retries > XHI_MAX_RETRIES)264break;265}266267dev_dbg(drvdata->dev, "done fifo_set_configuration\n");268269/*270* If the requested number of words have not been read from271* the device then indicate failure.272*/273if (remaining_words != 0)274return -EIO;275276return 0;277}278279/**280* fifo_icap_get_configuration - Read configuration data from the device.281* @drvdata: a pointer to the drvdata.282* @data: Address of the data representing the partial bitstream283* @size: the size of the partial bitstream in 32 bit words.284*285* This function reads the specified number of words from the ICAP device in286* the polled mode.287*/288int fifo_icap_get_configuration(struct hwicap_drvdata *drvdata,289u32 *frame_buffer, u32 num_words)290{291292u32 read_fifo_occupancy = 0;293u32 retries = 0;294u32 *data = frame_buffer;295u32 remaining_words;296u32 words_to_read;297298dev_dbg(drvdata->dev, "fifo_get_configuration\n");299300/*301* Check if the ICAP device is Busy with the last Write/Read302*/303if (fifo_icap_busy(drvdata))304return -EBUSY;305306remaining_words = num_words;307308while (remaining_words > 0) {309words_to_read = remaining_words;310/* The hardware has a limit on the number of words311that can be read at one time. */312if (words_to_read > XHI_MAX_READ_TRANSACTION_WORDS)313words_to_read = XHI_MAX_READ_TRANSACTION_WORDS;314315remaining_words -= words_to_read;316317fifo_icap_set_read_size(drvdata, words_to_read);318fifo_icap_start_readback(drvdata);319320while (words_to_read > 0) {321/* Wait until we have some data in the fifo. */322while (read_fifo_occupancy == 0) {323read_fifo_occupancy =324fifo_icap_read_fifo_occupancy(drvdata);325retries++;326if (retries > XHI_MAX_RETRIES)327return -EIO;328}329330if (read_fifo_occupancy > words_to_read)331read_fifo_occupancy = words_to_read;332333words_to_read -= read_fifo_occupancy;334335/* Read the data from the Read FIFO. */336while (read_fifo_occupancy != 0) {337*data++ = fifo_icap_fifo_read(drvdata);338read_fifo_occupancy--;339}340}341}342343dev_dbg(drvdata->dev, "done fifo_get_configuration\n");344345return 0;346}347348/**349* buffer_icap_reset - Reset the logic of the icap device.350* @drvdata: a pointer to the drvdata.351*352* This function forces the software reset of the complete HWICAP device.353* All the registers will return to the default value and the FIFO is also354* flushed as a part of this software reset.355*/356void fifo_icap_reset(struct hwicap_drvdata *drvdata)357{358u32 reg_data;359/*360* Reset the device by setting/clearing the RESET bit in the361* Control Register.362*/363reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);364365out_be32(drvdata->base_address + XHI_CR_OFFSET,366reg_data | XHI_CR_SW_RESET_MASK);367368out_be32(drvdata->base_address + XHI_CR_OFFSET,369reg_data & (~XHI_CR_SW_RESET_MASK));370371}372373/**374* fifo_icap_flush_fifo - This function flushes the FIFOs in the device.375* @drvdata: a pointer to the drvdata.376*/377void fifo_icap_flush_fifo(struct hwicap_drvdata *drvdata)378{379u32 reg_data;380/*381* Flush the FIFO by setting/clearing the FIFO Clear bit in the382* Control Register.383*/384reg_data = in_be32(drvdata->base_address + XHI_CR_OFFSET);385386out_be32(drvdata->base_address + XHI_CR_OFFSET,387reg_data | XHI_CR_FIFO_CLR_MASK);388389out_be32(drvdata->base_address + XHI_CR_OFFSET,390reg_data & (~XHI_CR_FIFO_CLR_MASK));391}392393394395